我刚刚开始使用 eclipse IDE 学习 java。我注意到 main 方法必须是静态的,否则会引发错误。因此,我必须为每个用户给定的输入声明许多 Scanner 类的对象。有没有办法让main方法成为非静态或者在eclipse中定义不带static关键字的main方法??
最佳答案
Is there a way to make the main method non-static or defining main method without the static keyword [...]?
不,这是 java 工作原理的一部分。 没有其他办法了。 但它不应该影响您的应用程序,因为您始终可以创建主类的实例并调用它的另一个方法:
public class X {
public static void main(String args[]) {
new X().nonStaticMain();
}
public void nonStaticMain() {
// just pretend this is your main
}
}
关于java - eclipse中的非静态main方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53901341/