当程序可以使用静态块在java 6中没有main时执行那么为什么需要main方法

时间:2021-12-29 20:48:53

for example-

   class A3{  
  static{  
  System.out.println("static block is invoked");  
  System.exit(0);  
  }  
}  

Someone explain me the reason.

有人向我解释原因。

4 个解决方案

#1


2  

A "program" cannot execute without main() (unless it is a servlet or an applet, in which case the web container or the web browser manages the corresponding entry point). In your example, a static block, it will only be executed once some other running class initializes (usually by referencing) the class A3.

没有main()就不能执行“程序”(除非它是servlet或applet,在这种情况下,Web容器或Web浏览器管理相应的入口点)。在您的示例中,一个静态块,只有在其他运行类初始化(通常通过引用)类A3时才会执行。

JLS-8.7. Static Initializers reads (in part)

JLS-8.7。静态初始化程序读取(部分)

A static initializer declared in a class is executed when the class is initialized (§12.4.2).

在类初始化时执行类中声明的静态初始化程序(第12.4.2节)。

#2


2  

Static blocks are executed when a Class is first initialized (after it is loaded). A main() method serves as an entry point for the application. Classes are (implicitly) loaded (and probably initialized) when they are first referenced.

首次初始化Class时(加载后)执行静态块。 main()方法用作应用程序的入口点。首次引用类时,(隐式)加载(并可能初始化)类。

See the difference?. You need to specify an entry point to a program / application by putting main() method in a class. A static initializer is run when a class is initialized, it is not the entry point of the program but a set of statements which are run (usually to do some setup work like initializing static fields (like maps)) when the class is being initialized.

看到不同?。您需要通过将main()方法放在类中来指定程序/应用程序的入口点。初始化类时运行静态初始化程序,它不是程序的入口点,而是在初始化类时运行的一组语句(通常用于执行某些设置工作,如初始化静态字段(如映射)) 。

#3


0  

if you want to print simple print statements static block is enough. but the real time program never contains system.out.println statements. and more over you can't access non static variables, methods..etc so it mean it is useless.

如果要打印简单的打印语句静态块就足够了。但实时程序从不包含system.out.println语句。而且你不能访问非静态变量,方法......等等,这意味着它没用。

#4


0  

The static block isn't necessarily invoked at program startup; it can be delayed to whenever the class is first initialized. It is necessary to specify a main class with a main function to indicate the entry point that should be invoked at program startup and because that's how Java works.

在程序启动时不一定要调用静态块;它可以延迟到每次初始化类时。有必要指定一个带有main函数的主类,以指示应该在程序启动时调用的入口点,因为这就是Java的工作原理。

#1


2  

A "program" cannot execute without main() (unless it is a servlet or an applet, in which case the web container or the web browser manages the corresponding entry point). In your example, a static block, it will only be executed once some other running class initializes (usually by referencing) the class A3.

没有main()就不能执行“程序”(除非它是servlet或applet,在这种情况下,Web容器或Web浏览器管理相应的入口点)。在您的示例中,一个静态块,只有在其他运行类初始化(通常通过引用)类A3时才会执行。

JLS-8.7. Static Initializers reads (in part)

JLS-8.7。静态初始化程序读取(部分)

A static initializer declared in a class is executed when the class is initialized (§12.4.2).

在类初始化时执行类中声明的静态初始化程序(第12.4.2节)。

#2


2  

Static blocks are executed when a Class is first initialized (after it is loaded). A main() method serves as an entry point for the application. Classes are (implicitly) loaded (and probably initialized) when they are first referenced.

首次初始化Class时(加载后)执行静态块。 main()方法用作应用程序的入口点。首次引用类时,(隐式)加载(并可能初始化)类。

See the difference?. You need to specify an entry point to a program / application by putting main() method in a class. A static initializer is run when a class is initialized, it is not the entry point of the program but a set of statements which are run (usually to do some setup work like initializing static fields (like maps)) when the class is being initialized.

看到不同?。您需要通过将main()方法放在类中来指定程序/应用程序的入口点。初始化类时运行静态初始化程序,它不是程序的入口点,而是在初始化类时运行的一组语句(通常用于执行某些设置工作,如初始化静态字段(如映射)) 。

#3


0  

if you want to print simple print statements static block is enough. but the real time program never contains system.out.println statements. and more over you can't access non static variables, methods..etc so it mean it is useless.

如果要打印简单的打印语句静态块就足够了。但实时程序从不包含system.out.println语句。而且你不能访问非静态变量,方法......等等,这意味着它没用。

#4


0  

The static block isn't necessarily invoked at program startup; it can be delayed to whenever the class is first initialized. It is necessary to specify a main class with a main function to indicate the entry point that should be invoked at program startup and because that's how Java works.

在程序启动时不一定要调用静态块;它可以延迟到每次初始化类时。有必要指定一个带有main函数的主类,以指示应该在程序启动时调用的入口点,因为这就是Java的工作原理。