2017-12-28 63 views
6

주제가 혼란스럽고 웹에서 찾을 수 없습니다. 제가 알기로, 프로그램이 시작될 때 클래스 로더는 .class 파일을로드하고 메모리에 Class 유형의 객체로 저장합니다.new 연산자를 사용하여 객체를 만들면 실제 .class 파일을 사용하여 Java에서 객체를 만듭니다.

Test test = new Test(); 

.class 파일을 사용하거나 메모리에 이미 Class 객체를 사용하여 만든 새 개체인가 : 우리가 사용하는 경우

내 질문은?

+2

불투명 한 부분은 * 프로그램이 시작될 때 클래스 로더가 클래스 파일을로드하고 Class 클래스의 객체로 메모리에 저장하는 것을 이해합니다. * –

+0

이 잘못 되었습니까? 그렇다면 실행주기를 배울 수있는 방향을 가리킬 수 있습니다. @YCF_L – rematnarab

답변

5

클래스가 JVM에로드되면 동일한 클래스 로더에 대해 동일한 클래스가 다시로드되지 않습니다. 새 인스턴스가 메모리의 클래스 객체에서 만들어집니다 (동일한 클래스 로더의 경우). 이미 클래스를로드하는 경우 (https://www.ibm.com/developerworks/java/tutorials/j-classloader/j-classloader.html에서 복사) 높은 수준의

  1. 전화 findLoadedClass에서

    단계를 확인합니다.

  2. 클래스를로드하지 않은 경우 findClass (클래스 로더 구현에서 재정의)를 사용하여 바이트를 가져옵니다.
  3. 원시 바이트가 발견되면 defineClass를 호출하여 클래스 객체로 변환합니다. (AppClassLoader의 예)
  4. 클래스가 해석되면 resolveClass를 호출하여 Class 객체를 확인합니다.
  5. 여전히 클래스가없는 경우 ClassNotFoundException을 발생시킵니다.

    protected synchronized Class<?> loadClass(String name, boolean resolve) 
    throws ClassNotFoundException 
    { 
        // First, check if the class has already been loaded 
        Class c = findLoadedClass(name); 
        if (c == null) { 
         try { 
          if (parent != null) { 
           c = parent.loadClass(name, false); 
          } else { 
           c = findBootstrapClass0(name); 
          } 
         } catch (ClassNotFoundException e) { 
          // If still not found, then invoke findClass in order 
          // to find the class. 
          c = findClass(name); 
         } 
        } 
        if (resolve) { 
         resolveClass(c); 
        } 
        return c; 
    } 
    

예와 동일한 해시 코드를 인쇄 할 다음의 모든과에로드되는 자세한 내용 http://www.onjava.com/pub/a/onjava/2005/01/26/classloading.html?page=1

<