>  기사  >  Java  >  Java의 인스턴스 제어 흐름

Java의 인스턴스 제어 흐름

WBOY
WBOY앞으로
2023-08-20 18:33:231470검색

Java의 인스턴스 제어 흐름

인스턴스 제어 흐름은 초보자는 물론 경험자도 반드시 알아야 할 Java 프로그래밍 언어의 기본 개념입니다. Java에서 인스턴스 제어 흐름은 클래스 내에 있는 멤버 실행의 단계별 프로세스입니다. 클래스 내부에 존재하는 멤버에는 인스턴스 변수, 인스턴스 메서드 및 인스턴스 블록이 포함됩니다.

Java 프로그램을 실행할 때마다 JVM은 main() 메서드를 먼저 찾은 다음 클래스를 메모리에 로드합니다. 더 나아가 클래스가 초기화되고 해당 정적 블록이 있는 경우 실행됩니다. 정적 블록을 실행한 후 인스턴스 제어 흐름이 시작됩니다. 이번 글에서는 인스턴스 제어 흐름(Instance Control Flow)이 무엇인지 설명하겠습니다.

Java의 인스턴스 제어 흐름

이전 섹션에서는 인스턴스 제어 흐름에 대해 간략히 설명했습니다. 이번 장에서는 예제 프로그램을 통해 자세히 다루겠습니다.

인스턴스 제어 흐름 프로세스에는 다음 단계가 포함됩니다.

    ㅋㅋㅋ
  • 두 번째 단계는 클래스의 인스턴스 변수를 실행하는 것입니다. 인스턴스 변수는 클래스 내부가 아닌 메서드 외부에서 선언됩니다. 일반적으로 해당 값을 표시하려면 생성자나 메서드를 정의해야 합니다.
  • 다음으로 인스턴스 블록은 클래스에 나타나는 순서대로 JVM에 의해 실행됩니다. 이러한 블록은 인스턴스 변수를 초기화하는 데 사용되는 익명 블록입니다.
  • 네 번째 단계에서 JVM은 클래스의 생성자를 호출하여 객체를 초기화합니다.
  • 进一步移动,调用实例方法来执行各自的操작품。
  • 결국 메모리 할당을 해제하기 위해 가비지 컬렉터가 호출됩니다.
  • 예 1
적중문翻译为:

示例 1

하단면에는 인스턴스가 없습니다.

输출

public class Example1 {
   int x = 10; // instance variable 
   // instance block
   {
      System.out.println("Inside an instance block"); 
   }
   // instance method
   void showVariable() {
      System.out.println("Value of x: " + x); 
   }
   // constructor
   Example1() {
      System.out.println("Inside the Constructor"); 
   }
   public static void main(String[] args) {
      System.out.println("Inside the Main method");
      Example1 exp = new Example1(); // creating object
      exp.showVariable(); // calling instance method
   }
}

예 2

적중문翻译为:

示例2

다음 예에서는 부모-자식 관계의 인스턴스 제어 흐름을 보여줍니다. Parent 클래스의 인스턴스 멤버는 Child 클래스의 인스턴스 멤버보다 먼저 실행됩니다.

Inside the Main method
Inside an instance block
Inside the Constructor
Value of x: 10

输출

// creating a parent class
class ExmpClass1 {
   int x = 10; // instance variable of parent
   // first instance block
   {
      System.out.println("Inside parent first instance block"); 
   }
   // constructor of parent class
   ExmpClass1() {
      System.out.println("Inside parent constructor"); 
      System.out.println("Value of x: " + this.x);
   }
   // Second instance block
   {
      System.out.println("Inside parent second instance block"); 
   }
}
// creating a child class
class ExmpClass2 extends ExmpClass1 {
   int y = 20; // instance variable of child
   // instance block of child
   {
      System.out.println("Inside instance block of child"); 
   }
   // creating constructor of child class
   ExmpClass2() {
      System.out.println("Inside child constructor"); 
      System.out.println("Value of y: " + this.y);
   }
}
public class Example2 {
   public static void main(String[] args) {
      // creating object of child class
      ExmpClass2 cls = new ExmpClass2(); 
      System.out.println("Inside the Main method");
   }
}

结论

이 글에서는 Java의 인스턴스 제어 흐름의 전체 프로세스를 이해했습니다. 이 과정에는 총 6단계가 있습니다. 기본적으로 인스턴스 제어 흐름은 Java Virtual Machine이 클래스 멤버를 실행하는 방법을 알려줍니다.

위 내용은 Java의 인스턴스 제어 흐름의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제