클래스를 초기화할 때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클래스에서 알 수 있다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
제4혹: 속성이 동적 귀속됩니까?모두가 알다시피 방법은 동태적으로 귀속될 수 있다. 하위 클래스가 상위 클래스로 전환될 때 가상 기기는 실행 대상의 진정한 유형을 찾을 수 있고 대상의 진정한 유형에 따라 하위 클래스를 호출하는 방법인지 하위 클래스...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.