Created
December 15, 2013 17:20
-
-
Save joa/7975574 to your computer and use it in GitHub Desktop.
Shadowing fun in Java The first references to x are this.x and afterwards the local variable. The code looks exactly the same but does something completely different.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Shadowing { | |
private float x; | |
public Shadowing(float x) { | |
this.x = x; | |
} | |
public void foo() { | |
if(x < 0.5f) { | |
x = Math.round(x); | |
System.out.println("x is "+x); | |
} | |
// ... | |
float x; | |
/// ... | |
x = Math.round(x); | |
System.out.println("x is "+x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment