0

0

Java中的实例控制流程

WBOY

WBOY

发布时间:2023-08-20 18:33:23

|

1577人浏览过

|

来源于tutorialspoint

转载

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控制流的整个过程。

Taygod免费企业建站系统 A1.1
Taygod免费企业建站系统 A1.1

TAYGOD免费企业建站系统是一款开源的免费程序,您可以 TAYGOD免费企业建站系统ASP版是一款基于asp+access的免费开源建站系统。整套系统的设计构造,完全考虑中小企业类网站的功能要求,网站后台功能强大,管理简捷,支持模板机制,能够快速建立您的企业网站。 系统特性: 采用流行的asp+access设计,功能强,实用性高。 代码美工完全分离,维护更方便。 对运行环境要求低,基本上一般的

下载
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
   }
}

输出

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

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");
   }
}

输出

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

结论

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.

相关文章

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
c++ 根号
c++ 根号

本专题整合了c++根号相关教程,阅读专题下面的文章了解更多详细内容。

22

2026.01.23

c++空格相关教程合集
c++空格相关教程合集

本专题整合了c++空格相关教程,阅读专题下面的文章了解更多详细内容。

24

2026.01.23

yy漫画官方登录入口地址合集
yy漫画官方登录入口地址合集

本专题整合了yy漫画入口相关合集,阅读专题下面的文章了解更多详细内容。

99

2026.01.23

漫蛙最新入口地址汇总2026
漫蛙最新入口地址汇总2026

本专题整合了漫蛙最新入口地址大全,阅读专题下面的文章了解更多详细内容。

132

2026.01.23

C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

15

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

65

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

61

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

63

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

33

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
SciPy 教程
SciPy 教程

共10课时 | 1.3万人学习

【web前端】Node.js快速入门
【web前端】Node.js快速入门

共16课时 | 2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号