首页 > Java > java教程 > 正文

Java中的实例控制流程

WBOY
发布: 2023-08-20 18:33:23
转载
1573人浏览过

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

PHP 网络编程技术与实例(曹衍龙)
PHP 网络编程技术与实例(曹衍龙)

PHP网络编程技术详解由浅入深,全面、系统地介绍了PHP开发技术,并提供了大量实例,供读者实战演练。另外,笔者专门为本书录制了相应的配套教学视频,以帮助读者更好地学习本书内容。这些视频和书中的实例源代码一起收录于配书光盘中。本书共分4篇。第1篇是PHP准备篇,介绍了PHP的优势、开发环境及安装;第2篇是PHP基础篇,介绍了PHP中的常量与变量、运算符与表达式、流程控制以及函数;第3篇是进阶篇,介绍

PHP 网络编程技术与实例(曹衍龙) 386
查看详情 PHP 网络编程技术与实例(曹衍龙)
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中的实例控制流程的详细内容,更多请关注php中文网其它相关文章!

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

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

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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