Skip to content

Instantly share code, notes, and snippets.

@joa
Created December 15, 2013 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joa/7975574 to your computer and use it in GitHub Desktop.
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.
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