Created
December 20, 2010 16:52
-
-
Save cocoatomo/748625 to your computer and use it in GitHub Desktop.
This file contains 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
public class A { | |
public static void main(String[] args) { | |
A a = new A() {}; | |
System.out.println(a.getClass()); | |
} | |
} |
This file contains 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
public class A { | |
private int a; | |
public A() {} | |
public void method() { | |
System.out.println("A"); | |
} | |
} | |
public class B { | |
A a = new A() { // anonymous subclass | |
public void method() { System.out.println("AA"); } | |
}; | |
} | |
public class C { | |
static int s; | |
static { s = 1; } // static initializer | |
private int i; | |
{ i = 2; } // instance initializer | |
} | |
public class D { | |
List list = new ArrayList{ // anonymous subclass | |
{add(0); add(1); add(2);} // instance initializer -> [0, 1, 2] | |
}; | |
} |
This file contains 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
public class Initializer { | |
private static String message = "I am Initializer."; | |
private String name = "Initializer"; | |
static { | |
System.out.println("Change message: " + message); | |
message = "message is changed."; | |
} | |
{ | |
System.out.println("Change name: " + name); | |
name = "new name"; | |
} | |
public static void main(String[] args) { | |
Initializer initializer = new Initializer(); | |
System.out.println("message: " + message + | |
" name: " + initializer.name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment