`
bo521dai
  • 浏览: 18927 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

java 陷阱

阅读更多
猜猜输出什么?

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。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics