Created
February 19, 2014 15:18
-
-
Save caoxudong/9094098 to your computer and use it in GitHub Desktop.
该类用于验证类的初始化时机
This file contains hidden or 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
调用非自己声明的方法,不会执行类初始化 | |
Class TechnequeGuy initialized | |
Person says, 'I'm a techneque guy' | |
--------------- | |
给非自己声明的静态域赋值,不会执行类初始化 | |
--------------- | |
访问自己声明的常量,不会执行类初始化 | |
Programmer is really cute? Yes | |
--------------- | |
以反射方式调用自己声明的静态方法,会执行类初始化 | |
Class Programmer initialized | |
Programmer says 'bla bla bla...' | |
--------------- | |
以反射方式调用非自己声明的静态方法,不会执行类初始化 | |
Person says, 'I'm a techneque guy' | |
--------------- |
This file contains hidden or 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
package problem.java.lang.klass; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
/** | |
* 该类用于验证类的初始化时机 | |
* | |
* 具体时机参见JLS http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.1 | |
* | |
*/ | |
public class TestClassInitialization { | |
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { | |
//调用非自己声明的方法,不会执行类初始化 | |
System.out.println("调用非自己声明的方法,不会执行类初始化"); | |
Programmer.print(); | |
System.out.println("---------------"); | |
//给非自己声明的静态域赋值,不会执行类初始化 | |
System.out.println("给非自己声明的静态域赋值,不会执行类初始化"); | |
Programmer.isSmart = true; | |
System.out.println("---------------"); | |
//访问自己声明的常量,不会执行类初始化 | |
System.out.println("访问自己声明的常量,不会执行类初始化"); | |
System.out.println("Programmer is really cute? " + (Programmer.isCute?"Yes":"Noe")); | |
System.out.println("---------------"); | |
//以反射方式调用自己声明的静态方法,会执行类初始化 | |
System.out.println("以反射方式调用自己声明的静态方法,会执行类初始化"); | |
Class<?> programmerClass = Programmer.class; | |
Method talkMethod = programmerClass.getMethod("talk", ( Class<?>[])null); | |
talkMethod.invoke(programmerClass, (Object[])null); | |
System.out.println("---------------"); | |
//以反射方式调用非自己声明的静态方法,不会执行类初始化 | |
System.out.println("以反射方式调用非自己声明的静态方法,不会执行类初始化"); | |
Class<?> programmerAmateurClass = Programmer.class; | |
Method printMethod = programmerAmateurClass.getMethod("print", ( Class<?>[])null); | |
printMethod.invoke(programmerAmateurClass, (Object[])null); | |
System.out.println("---------------"); | |
//暂时还没想到单纯靠断言启动类初始化的方法 | |
} | |
} | |
interface Person { | |
} | |
class TechnequeGuy implements Person { | |
public static boolean isSmart = false; | |
static { | |
System.out.println("Class TechnequeGuy initialized"); | |
} | |
public static void print() { | |
System.out.println("Person says, 'I'm a techneque guy'"); | |
} | |
} | |
class Programmer extends TechnequeGuy implements Person { | |
public static final boolean isCute = true; | |
public static void talk() { | |
System.out.println("Programmer says 'bla bla bla...'"); | |
} | |
static { | |
System.out.println("Class Programmer initialized"); | |
} | |
} | |
class ProgrammingAmateur extends Programmer implements Person { | |
public static boolean isRealyRealyCute = true; | |
static { | |
boolean isRealyCute = true; | |
assert isRealyCute != false; | |
if (isRealyCute) { | |
System.out.println("Class ProgrammingAmateur initialized, and he's really cute"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment