Skip to content

Instantly share code, notes, and snippets.

@kohsuke
Created June 15, 2016 08:47
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 kohsuke/1a03c7fd834f2941a7f7ec784123e397 to your computer and use it in GitHub Desktop.
Save kohsuke/1a03c7fd834f2941a7f7ec784123e397 to your computer and use it in GitHub Desktop.
Java Puzzler of the day
public class Foo {
public static class A {
public int i = 1;
}
public static class B extends A {
public int i = 2;
}
public static void main(String[] args) {
B b = new B();
A a = b;
// Quiz: what do they print?
System.out.println(b.i);
System.out.println(a.i);
}
}
@agentgt
Copy link

agentgt commented Jun 16, 2016

I have often wondered if Java field dispatching (err lack of it... sorry for the spoiler) was chosen because of performance reasons or for semantic reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment