Home> Java> javaTutorial> body text

Instance control flow in Java

WBOY
Release: 2023-08-20 18:33:23
forward
1453 people have browsed it

Instance control flow in Java

Instance control flow is a fundamental concept of Java programming language that a beginner, as well as an experienced one, must know about. In Java, the instance control flow is a step by step process of execution of members lies within the class. The members that exist inside a class include instance variables, instance methods, and instance blocks.

Whenever we execute a Java program, JVM looks for the main() method first and then, it loads the class into memory. Moving further, the class gets initialized and its static block, if any, is executed. After the execution of the static block, the instance control flow begins. In this article, we are going to explain what is an Instance Control Flow.

Instance Control Flow in Java

In the previous section, we briefed about the instance control flow. In this section, we will discuss it in detail through example programs.

The following steps are involved in the process of Instance Control Flow:

  • 第一步是从类的顶部到底部识别实例成员,如实例变量、实例方法和实例块

  • The second step is the execution of instance variable of the class. The instance variables are declared inside the class but outside the method. Generally, to show their values we need to define either a constructor or a method.

  • Next, the instance blocks get executed by JVM in the order they appear in the class. These blocks are anonymous blocks that are used to initialize instance variables.

  • In the fourth step, the JVM invokes the constructor of the class to initialize the object.

  • 进一步移动,调用实例方法来执行各自的操作。

  • In the end, the garbage collector is called to deallocate the memory.

Example 1

的中文翻译为:

示例 1

下面的示例演示了Instance控制流的整个过程。

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 } }
Copy after login

输出

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

Example 2

的中文翻译为:

示例2

The following example illustrates the Instance control flow in parent-child relationship. The instance members of the Parent class are gets executed before those of the Child class.

// 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"); } }
Copy after login

输出

Inside parent first instance block Inside parent second instance block Inside parent constructor Value of x: 10 Inside instance block of child Inside child constructor Value of y: 20 Inside the Main method
Copy after login

结论

In this article, we have understood the whole process of Instance Control Flow in Java. There are a total of six steps in this process. Basically, the Instance Control Flow tells us how the Java Virtual Machine executes the members of a class.

The above is the detailed content of Instance control flow in Java. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!