java 静态块会在没有 main 方法的情况下执行吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17889220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 17:53:25  来源:igfitidea点击:

Will a static block execute without the main method?

java

提问by Vijin Paulraj

I'm trying to run the below code on my machine, but it didn't execute anything nor showing any errors.

我正在尝试在我的机器上运行以下代码,但它没有执行任何操作,也没有显示任何错误。

public class StaticBlockDemo {
    static {
        System.out.println("Hello World");
    }
}

Can someone please help me? By the way, I'm using Java 7.

有人可以帮帮我吗?顺便说一下,我使用的是 Java 7。

回答by arshajii

If you put a System.exit(0)at the end of the static-block, it will run with no errors in Java 6 and below (without a valid main!). This is because the staticblock is executed beforea valid mainmethod is searched for, so if you exit the program at the end of the static block, you will receive no errors.

如果将 aSystem.exit(0)放在static-block的末尾,它将在 Java 6 及更低版本中运行而不会出错(没有有效的main!)。这是因为static块是main搜索到有效方法之前执行的,所以如果您在静态块的末尾退出程序,您将不会收到任何错误。

However,this behavior was changed in Java 7; now you must include an explicit main, even if it might never be reached.

但是,这种行为在 Java 7 中发生了变化;现在您必须包含一个明确的main,即使它可能永远无法到达。

In Java 7, the answer to the question is false, but in Java 6 and below the answer is indeed true.

在 Java 7 中,问题的答案是false,但在 Java 6 及以下版本中,答案确实是true



public class Test {
    static {
        System.out.println("Hello World");
        System.exit(0);
    }
}

Java 6:

爪哇 6:

Hello World

Java 7:

爪哇7:

Error: Main method not found in class Test, please define the main method as:
   public static void main(String[] args)

回答by Mike Samuel

Static blocks execute when a class is initialized. Normally, the mainclass will cause initialization of the bootstrap class, but there are other ways to bootstrap a program, for example via a VM's native embedding API.

静态块在类初始化时执行。通常,main该类会导致 bootstrap 类的初始化,但还有其他方法来引导程序,例如通过 VM 的本机嵌入 API

Invoking the static mainmethod of a class causes its initialization, but so do many other things:

调用static main一个类的方法会导致它的初始化,但还有很多其他的事情:

  1. creating an instance of that class,
  2. calling any other static method,
  3. reading a static field (that is not final or has type other than a primitive type or String).
  1. 创建该类的实例,
  2. 调用任何其他静态方法,
  3. 读取静态字段(不是最终的或具有原始类型或字符串以外的类型)。

For more detail see the JLS chapter 12.4

有关更多详细信息,请参阅JLS 第 12.4 章

The below shows this in action

下面显示了这一点

public class Foo {
  static { System.out.println("Foo initialized"); }

  public static void main(String... argv) {
    Initialized.callingThisCausesClassInitialization();
  }

  static class Initialized {
    static { System.out.println("Initialized initialized"); }
    static void callingThisCausesClassInitialization() {}
  }

  static class NotInitialized {
    static { System.out.println("NotInitialized initialized"); }
    static void callingThisCausesClassInitialization() {}
  }
}

Running foo will print

运行 foo 将打印

Foo initialized
Initialized initialized

It will not print

它不会打印

NotInitialized initialized

because nothing is done during the execution of that program that causes its initialization.

因为在导致其初始化的程序执行期间没有做任何事情。

It looks like your class has that behavior because it is never used, like NotInitializedabove.

看起来你的类有这种行为,因为它从未被使用过,就像NotInitialized上面一样。

回答by Türkmen Mustafa Demirci

In java 8 you can not run a program without explicitly write a main method. From this angle the answer false. Static block is not executed without main method.Below is a piece of code which shows the order of initializaion .(Static block==>main==>initialization block==>constructor)

在 java 8 中,如果不明确编写 main 方法,您将无法运行程序。从这个角度答案是错误的。没有main方法就不会执行静态块。下面是一段代码,显示了初始化的顺序。(静态块==>main==>初始化块==>构造函数)

public class StaticBlock {

static{
    System.out.println("in static block");
}


{
    System.out.println("in initialization block");
}

public StaticBlock(){
    System.out.println("in const");
}

public static void main(String[] args) {
        System.out.println("in main method");
        StaticBlock block=new StaticBlock();
}

in static block
in main method
in initialization block
in const


main 方法中的静态块中 在 const
中的初始化块

回答by ankita

abstract class test extends javafx.application.Application {
    static {
        System.out.println(“hello”);
        System.exit(0);
    }
}

Compile using : javac -source 1.6 test.java

编译使用: javac -source 1.6 test.java

Run using : java test

运行使用: java test

This will even work with JDK 1.8.

这甚至适用于 JDK 1.8。

回答by user176326

The most voted answer is 'mostly' correct, but not entirely. Consider the following code below where the class has a main method and a static method. The static method WILL execute (successfully) BEFORE the main method and constructor and produces the sequence: A B C D - which is not as you might have thought.

投票最多的答案是“大部分”正确,但不完全正确。考虑下面的代码,其中类有一个 main 方法和一个静态方法。静态方法将在主方法和构造函数之前执行(成功)并产生序列:ABCD - 这与您可能想象的不同。

public class Sequence {
    Sequence() {
        System.out.print("c ");
    }
    {
        System.out.print("B ");
    }
    public static void main(String[] args) {
        new Sequence().go();
    }
    void go() {
        System.out.print("D ");
    }
    static{
        System.out.print("A ");
    }
}

回答by NAGHMAAN MOHASEEN

the explanation is quite in detail but iam abstracting it for this specific question first of all a class is loaded by the class loader subsystem when it refers to it for the first time in runtime and not compile time. so the ClassLoader itself is a class in java.lang package and

解释非常详细,但我首先针对这个特定问题将它抽象出来,当它在运行时而不是编译时第一次引用它时,类加载器子系统会加载它。所以 ClassLoader 本身是 java.lang 包中的一个类,并且

its instances are called as classLoader instances that load the classes now coming to details of this it follows an hierarchy with the BootStrap ClassLoader being at the top

它的实例被称为加载类的类加载器实例,现在详细介绍它遵循一个层次结构,BootStrap ClassLoader 位于顶部

note the BootStrap class loader itself is an instance of ClassLoader.

请注意 BootStrap 类加载器本身是 ClassLoader 的一个实例。

more ever these instances also perform verification ,preparing ,resolving symbolic references to keep it simple it performs dynamic linking of your java program.

这些实例还执行验证、准备、解析符号引用以保持简单,它执行 Java 程序的动态链接。

now when your compiler compiles .java file it inserts a public static final field of type Class you can access it in your java.lang.Class obj=name_of_your_class.class

现在,当您的编译器编译 .java 文件时,它会插入一个 Class 类型的公共静态 final 字段,您可以在 java.lang.Class obj=name_of_your_class.class 中访问它

and it contains a method getClassLoader which tell the classLoader Instance that loaded this class.

并且它包含一个方法 getClassLoader ,它告诉加载这个类的 classLoader 实例。

fine its only by using this information the ClassLoaders get to know the name of the class to be loaded ie its fully qualified name(package.class)

很好,只有通过使用这些信息,ClassLoader 才能知道要加载的类的名称,即它的完全限定名称(package.class)

now before searching for a file in its native file system and loading it it checks with its parent instance that it has already loaded that file nad this checking propagates all the way top BootclassLoader only if none of them have loaded it then only that instance loads that classfile . the details of how these things happen is irrelevant in this context.

现在在其本地文件系统中搜索文件并加载它之前,它会检查其父实例是否已经加载了该文件 nad 此检查一直传播 top BootclassLoader 仅当它们都没有加载它时,只有该实例加载该文件类文件。在这种情况下,这些事情如何发生的细节无关紧要。

and once the class is loaded the static block is executed in the initialization phase of class loader subsystem.

一旦类被加载,静态块就会在类加载器子系统的初始化阶段执行。

since i have already told that its the compiler that plays the role of inserting that field. and note that not finding main method is an runtime error so the compiler is not responsible for that rather it is the JVM .

因为我已经告诉过它的编译器起着插入该字段的作用。并注意没有找到 main 方法是一个运行时错误,所以编译器不对此负责,而是 JVM 。

from java 7 the main method is searched before and if we dont have we get this error in runtime but in java 6 and earlier version when class loading had taken place there itself the static block was exectued and then it searched to find the main method but if we provide System.exit(0) ; in the block it terminates the program even before search is made hence we did not have any error

从 java 7 开始之前搜索 main 方法,如果我们没有,我们会在运行时收到此错误,但在 java 6 和更早版本中,当类加载发生在那里时,静态块被执行,然后它搜索以找到 main 方法,但是如果我们提供 System.exit(0) ;在块中它甚至在搜索之前就终止了程序,因此我们没有任何错误

although in java 7 probing for the main method is done before execution of the static block the execution of static block itself depends on the successful finding of main method.even though the order of execution of program is program is same as in java 6 and earlier edition.

尽管在 java 7 中对 main 方法的探测是在执行静态块之前完成的,但静态块本身的执行取决于 main 方法的成功找到。即使程序的执行顺序与 java 6 及更早版本中的程序相同版。

further in detail the first element to be placed on the stack itself should be the main thread. and we know how the execution control flows and its even important to note that static blocks even if they are containing variable local to their block they are never placed as an activation record on the stack but rather on the method area. so from java 7 the JVM checks for the existence of main thread record on the stack then giving the control to the static blocks after which it executes all the static blocks in its order . whereas it did the reverse in java 6 and earlier edition

更详细地说,第一个放在堆栈上的元素应该是主线程。并且我们知道执行控制是如何流动的,甚至重要的是要注意静态块,即使它们包含其块的局部变量,它们也不会作为活动记录放置在堆栈上,而是放在方法区中。因此,从 Java 7 开始,JVM 检查堆栈上是否存在主线程记录,然后将控制权交给静态块,然后按顺序执行所有静态块。而它在 java 6 和更早的版本中做了相反的事情

its also important to note we cannot nest a static block within any static or non static scope as this block has its only higher enclosed scope to be that of the class

同样重要的是要注意我们不能在任何静态或非静态范围内嵌套一个静态块,因为这个块只有更高的封闭范围是类的范围

i even know its a bit tricky to understand it but reading this answer will definitely provide a good ,accurate and deep understanding of static blocks .

我什至知道理解它有点棘手,但阅读这个答案肯定会提供对静态块的良好、准确和深入的理解。