英文:
Why does the public static void main method not automatically run in an instantiated class?
问题
我理解在我的起始类中的 public static void main
的目的 - 通过在程序运行时执行代码。
我阅读了一些在线文章,其中提到任何类都可以有一个主类。我创建了一个从第一个类调用并创建了一个实例的第二个类,但是第二个类主方法中的代码没有执行。我是否对于这个方法在非主要类中的工作方式有误解?
public class Main {
public static void main(String[] args) {
aClass newClass = new aClass();
}
}
public class aClass{
public static void main(String[] args) {
System.out.println("hello");
}
}
英文:
I understand the point of public static void main
in my starting class - to have code which is executed when the program is run.
Some articles I read online state that any class can have a main class. I created a second class which is called from the first class and created an instance of it, but the code in the second classes main method doesn't run. Am I misunderstanding how this method works in a class other than the primary class?
public class Main {
public static void main(String[] args) {
aClass newClass = new aClass();
}
}
public class aClass{
public static void main(String[] args) {
System.out.println("hello");
}
}
答案1
得分: 2
"任何类都可以有一个主类"是真的。但是只会运行一个 main
方法,不管有多少个类有这个方法。
哪个main
方法被运行取决于应用程序的指定入口点,即在通过java name.of.class
运行代码时您显式指定的入口点,或者您的JAR清单指示的入口点是什么。
相反,您可以为类提供一个静态初始化块。该块将在加载类的第一次时运行:
public class Main {
public static void main(String[] args) {
new aClass();
new aClass();
}
}
public class aClass{
static {
// 仅运行一次!
System.out.println("hello");
}
}
即使在您的代码中没有实例化您的类,但此代码也会运行,比如说,在其上调用静态方法。但是,如果您的代码中没有任何内容与aClass
有关,那么它的静态初始化块将不会被运行。
英文:
It’s true that “any class can have a main class”. But only one main
method is run, regardless of how many classes have one.
And which main
method is run depends on the designated entry point of your application, i.e. what you explicitly indicate as the entry point when running your code via java name.of.class
, or whatever your JAR manifest indicates as being the entry point.
Conversely, you can provide a class with a static initialisation block. This block will be run once, the first time the class is loaded:
public class Main {
public static void main(String[] args) {
new aClass();
new aClass();
}
}
public class aClass{
static {
// Run only once!
System.out.println("hello");
}
}
This code will run even when you don’t instantiate your class but, say, call a static method on it. But if nothing in your code ever refers to aClass
at all, its static initialiser will not be run.
答案2
得分: 1
main
方法由 JVM 在一个类上调用(当然,除非你显式地调用它)。
如果你希望在实例化 aClass
时调用代码,你需要将代码移到构造函数中:
public class aClass{
public aClass() {
System.out.println("hello");
}
}
或者,当然也可以显式地调用 aClass.main
:
public class Main {
public static void main(String[] args) {
aClass.main(args);
}
}
英文:
The main
method is called on one class by the JVM (unless you call it explicitly, of course).
If you want the code to be called when you instantiate aClass
, you need to move the code to the constructor:
public class aClass{
public aClass() {
System.out.println("hello");
}
}
or, of course, call aClass.main
explicitly:
public class Main {
public static void main(String[] args) {
aClass.main(args);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论