[color=darkred][align=center][size=medium]猜猜输出什么?[/size][/align][/color]
class Test{
Test(){
do1();
}
public void do1(){
}
}
class Farther extends Test{
Integer flag=new Integer(3);
Farther(){
System.out.println(flag);
}
public void do1()
{
System.out.println(flag);
}
public static void main(String args[]){
new Farther();
}
}
输出:
null
3
-------------------------------------------------------------
-------------------------------------------------------------
-------------------------------------------------------------
-------------------------------------------------------------
interface Angry {
String greeting = "Grrrr!";
int angerLevel = Dog.getAngerLevel();
}
class Dog implements Angry {
public static final String greeting = "Wong, Wong, Wong!";
static {
System.out.println("Dog was initialized.");
}
public static int getAngerLevel() {
System.out.println("Angry was initialized.");
return 1;
}
}
class Main {
public static void main(String[] args) throws Exception {
testClassInit();
}
public static void testClassInit() throws Exception {
//passive use of Angry
System.out.println(Angry.greeting);
//passive use of Dog
System.out.println(Dog.greeting);
}
}
输出:
Grrrr!
Wong, Wong, Wong!
在 inside the jvm 书中,jvm对static final修饰的 变量,在其他类中只是用类名.Field,则只是简单 替换,而根本不需加载class。
class Test{
Test(){
do1();
}
public void do1(){
}
}
class Farther extends Test{
Integer flag=new Integer(3);
Farther(){
System.out.println(flag);
}
public void do1()
{
System.out.println(flag);
}
public static void main(String args[]){
new Farther();
}
}
输出:
null
3
-------------------------------------------------------------
-------------------------------------------------------------
-------------------------------------------------------------
-------------------------------------------------------------
interface Angry {
String greeting = "Grrrr!";
int angerLevel = Dog.getAngerLevel();
}
class Dog implements Angry {
public static final String greeting = "Wong, Wong, Wong!";
static {
System.out.println("Dog was initialized.");
}
public static int getAngerLevel() {
System.out.println("Angry was initialized.");
return 1;
}
}
class Main {
public static void main(String[] args) throws Exception {
testClassInit();
}
public static void testClassInit() throws Exception {
//passive use of Angry
System.out.println(Angry.greeting);
//passive use of Dog
System.out.println(Dog.greeting);
}
}
输出:
Grrrr!
Wong, Wong, Wong!
在 inside the jvm 书中,jvm对static final修饰的 变量,在其他类中只是用类名.Field,则只是简单 替换,而根本不需加载class。