클래스를 초기화할 때final로 수식된 정적 필드의 표현 방식

2453 단어 가상 시스템
package cantellow.text8;   
  
public class TestStaticFinal   
{   
    /**   
     * @author cantellow  
     */  
    public static void main(String[] args)   
    {   
        // TODO Auto-generated method stub   
        String var1 = StaticFinal.stringStaticFinal;   
        System.out.println(var1);   
        String var2 = Static2.stringStatic;   
        System.out.println(var2);   
           
        StaticFinal.finalprint();   
        Static2.Print();   
    }   
}   
  
class StaticFinal   
{   
    final static String stringStaticFinal = "This is Static and Final string";   
    static  
    {   
        System.out.println("     StaticFinal             ");       
        System.out.println(stringStaticFinal);     
    }   
    public final static void finalprint()   
    {   
        System.out.println("This is finalprint()");    
    }      
}   
class Static2   
{   
    static String stringStatic = "This is static string";   
    static  
    {   
        System.out.println("     Static2             ");   
        System.out.println(stringStatic);          
    }   
    public static void Print()   
    {   
        System.out.println("This is print()");     
    }   
}  

 
결과 출력:
This is Static and Final string   
     Static2                
This is static string   
This is static string   
     StaticFinal                
This is Static and Final string   
This is finalprint()   
This is print()  

 
질문: StaticFinal 클래스의stringStaticFinal 필드에 접근할 때 StaticFinal 클래스가 초기화되지 않은 이유는 무엇입니까?
==============================================================
참고 사항:
모든 자바 가상기는 모든 클래스나 인터페이스가 처음 주동적으로 사용될 때 초기화해야 한다. 특정한 클래스의 정적 방법과 정적 필드를 사용할 때 가상기는 이 클래스나 인터페이스가 초기화되었는지 확인해야 한다. 초기화되지 않으면 초기화해야 한다.단,final로 장식된 정적 필드를 제외하고는 컴파일할 때의 상수 표현식으로 초기화됩니다.그래서 우리는 StaticFinal 클래스의stringStaticFinal 필드에 접근했을 때staticFinal 클래스가 초기화되지 않았다는 것을 보았다.final 수식이 없는 Static2 클래스의 정적 필드에 접근할 때 Static2 클래스의 초기화를 초래했다.그리고 정적 방법은final 필드 수식이 있든 없든 클래스의 초기화를 일으킨다.한 번 초기화된 클래스는 다시 초기화되지 않는다는 점도 Static2클래스에서 알 수 있다.

좋은 웹페이지 즐겨찾기