Skip to content

Instantly share code, notes, and snippets.

@cyrilmottier
Created January 10, 2013 09:16
Show Gist options
  • Save cyrilmottier/4500676 to your computer and use it in GitHub Desktop.
Save cyrilmottier/4500676 to your computer and use it in GitHub Desktop.
Java puzzle: field instantiation
package com.cyrilmottier.test;
public class ClassA {
public ClassA() {
System.out.println("ClassA()");
method();
}
public void method() {
System.out.println("ClassA.method()");
}
}
package com.cyrilmottier.test;
public class ClassB extends ClassA {
private static final Point POINT = new Point(0, 0);
private final String mString = "Hello world";
private final Point mPoint = new Point(1, 1);
public ClassB() {
super();
System.out.println("ClassB(): " + POINT + " / " + mString + " / " + mPoint);
}
@Override
public void method() {
System.out.println("ClassB.method(): " + POINT + " / " + mString + " / " + mPoint);
super.method();
}
public static void main(String[] args) {
new ClassB();
}
}
package com.cyrilmottier.test;
public class Point {
public final int x;
public final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point(" + x + ", " + y + ")";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment