Atom Feed SITE FEED   ADD TO GOOGLE READER

Don't do this: Share names

The following program is valid Java, even though the Runnable on line 1 is a completely different symbol than Runnable on line 3:
public class Refrigerator implements Runnable {
public void run() {
new Runnable().freeze();
}

public class Runnable {
void freeze() {
System.out.println("cold and refreshing");
}
}

public static void main(String[] args) {
new Thread(new Refrigerator()).start();
}
}
I'm not quite sure if this is shadowing, obscuring or hiding, but it's certainly not good. Writing a compiler must be pretty difficult!
For what it's worth, this is also a valid (albeit useless) Java program:
class Foo<Bar extends Foo> {
  Foo<Foo> Foo = new Foo<Foo>();
  Foo() {
    Foo.Foo = new Foo<Foo>().Foo.Foo(Foo);
  }
  <Foo extends Bar> Foo Foo(Bar Foo) {
    return (Foo) Foo.Foo(Foo);
  }
}
:D
Jesse: A more useless piece of Java code will be hard to find indeed!
About name scope in a compiler (the Scope class in Javac is quite nice), just imagine what closure is going to do to it!