AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
# 执行顺序 > 原文: [https://javabeginnerstutorial.com/core-java-tutorial/order-of-execution-of-blocks-in-java/](https://javabeginnerstutorial.com/core-java-tutorial/order-of-execution-of-blocks-in-java/) 在本文中,我们将学习 Java 中块执行的顺序。 Java 类中的不同块及其执行顺序 1. 静态块 2. 初始化块(匿名块) 3. 构造器 ```java /* * Here we will learn to see how the different part (Ananymous Block, Constructor and Static Block ) of class will behave * and what would be the order of execution. */ class JBTCLass { /* * Here Creating the Ananymous Block */ { System.out.println("Inside Ananymous Block"); } /* * Now Creating the Static Block in Class */ static { System.out.println("Inside Static Block"); } /* * Here Creating the Constructor of Class */ JBTCLass() { System.out.println("Inside Constructor of Class"); } public static void main(String[] args) { // Creating the Object of the Class JBTCLass obj = new JBTCLass(); System.out.println("*******************"); // Again Creating Object of Class JBTCLass obj1 = new JBTCLass(); } } ``` 上面程序的输出 ```java Inside Static Block Inside Ananymous Block Inside COnstructor of Class ******************* Inside Ananymous Block Inside COnstructor of Class ``` 如您所见,`STATIC`块仅在加载类时执行一次。 但是,每当创建一个类的对象时,匿名块和构造器就会运行。 初始化块将首先执行,然后构造器。