Java 中的每个程序都需要一个类吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5204385/
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-10-30 09:56:35  来源:igfitidea点击:

Does every program in Java require a class?

java

提问by Tony

Every Java program requires the presence of at least one class.

每个 Java 程序都需要至少存在一个类。

Is the above statement always true ?

上面的说法总是正确的吗?

回答by rlibby

Yes, you need at least one class to have a program, but no, you do not need any methods (contrary to some other answers).

是的,您至少需要一个班级才能拥有一个程序,但,您不需要任何方法(与其他一些答案相反)。

The reason you need a class is because in Java, all code is inside classes. So to have any code, you need a class. However, code doesn't necessarily need to be in a method. It can also be in initializers. So, here is a complete Java program with no methods:

您需要类的原因是因为在 Java 中,所有代码都在类中。所以要拥有任何代码,你需要一个类。但是,代码不一定需要在方法中。它也可以在初始化程序中。所以,这是一个没有方法的完整 Java 程序:

class LookMaNoMethods {
    static {
        System.out.println("Hello, world!");
        System.exit(0);
    }
}

And that gives...

这给...

$ javac LookMaNoMethods.java 
$ java LookMaNoMethods 
Hello, world!
$ 

EDIT : From Java 7 the above code with just static block and no main method does not produce any output. Main method is now compulsory. The code with no main method compiles successfully though.

编辑:从 Java 7 开始,上面只有静态块且没有 main 方法的代码不会产生任何输出。Main 方法现在是强制性的。虽然没有 main 方法的代码编译成功。

回答by Sanjay T. Sharma

From the JVM's point of view; yes. From a programmers view point, it can be a Class or an Enum.

从JVM的角度来看;是的。从程序员的角度来看,它可以是一个类或一个枚举。

public enum AAA {

    AAA;

    public static void main(final String[] args) {
        System.out.println("H");
    }

}

EDIT: Even if you have a class with empty main method, there are a lot of core classes which work behind the scene to just run the "empty" class of yours. A list of those classes (around 200 from the java.*package) can be viewed by setting the -verbose:classJVM parameter.

编辑:即使你有一个空的 main 方法的类,也有很多核心类在幕后工作,只是运行你的“空”类。java.*可以通过设置-verbose:classJVM 参数查看这些类的列表(来自包的大约 200 个)。

回答by Matti Virkkunen

A program requires an entry point. An entry point has to be a method. In Java, every method must be contained in a class.

一个程序需要一个入口点。入口点必须是一个方法。在 Java 中,每个方法都必须包含在一个类中。

That would imply that every program must have a at least one class.

这意味着每个程序必须至少有一个类。

回答by Konerak

Yes. In Java you always need one class with the function main to have the JRE run it.

是的。在 Java 中,您总是需要一个带有 main 函数的类来让 JRE 运行它。

回答by stinepike

yes , you need minimum one class.

是的,你至少需要一门课。

回答by JATIN BEHUNE

JAVA required at least one class in a program because at the time of execution of Java programs we needed to provide the name of a class which contains the main () method.
so, it is compulsory to provide at least one class name to Java programs. ex--->`

JAVA 要求程序中至少有一个类,因为在执行 Java 程序时,我们需要提供包含 main() 方法的类的名称。
因此,必须为 Java 程序提供至少一个类名。例如--->`

class Test 
{ 
public static void main(String [] args)
{ 
System.out.println("Hello World");
}
}

so, javac _____ ("Here we have to give the name of java program in which we save")

所以,javac _____(“这里我们必须给出我们保存的java程序的名称”)

java ______ ("Provide the name of a class which contain the main() method")

java ______ ("提供包含 main() 方法的类的名称")

-----> according to our program

-----> 根据我们的程序

javac Hello (here I save the program name by Hello.java)

javac Hello(这里我用Hello.java保存程序名)

java Test (because Test class contains main() method)

java Test(因为Test类包含main()方法)

Thank You

谢谢