可以不使用public static void main(String[] args)来编写Java代码吗?

huangapple go评论62阅读模式
英文:

Is it possible to write Java code without public static void main(String[] args)?

问题

我正在准备一个非常基础/初学者级别的Java考试在复习一些示例代码时我注意到有一些示例代码没有包含方法public static void main(String[] args)

这是怎么可能的我是不是漏掉了什么

教授上传的没有该方法的示例代码示例

public class Student {

  int midtermExam;
  int finalExam;
 
  double calcAvg() {
    double returnValue;
    returnValue = (midtermExam + finalExam) / 2.0;
    return returnValue;
  }

  char getLetterGrade() {

    char grade;
    double avg = (midtermExam + finalExam) / 2.0;
    // double avg = calcAvg();
    
    if (avg >= 90)
      grade = 'A';
    else if (avg >= 80)
      grade = 'B';
    else if (avg >= 70)
      grade = 'C';
    else
      grade = 'F';

    return grade;
  }

}
英文:

I am studying for a very basic / beginners Java exam, and when reviewing some example code, I noticed a couple that did not include the method: public static void main(String[] args)

How is this possible? Is there something I am missing?

Example of example code my professor uploaded without the method:

public class Student {

  int midtermExam;
  int finalExam;
 
  double calcAvg() {
    double returnValue;
    returnValue = (midtermExam + finalExam) / 2.0;
    return returnValue;
  }

  char getLetterGrade() {

    char grade;
    double avg = (midtermExam + finalExam) / 2.0;
    // double avg = calcAvg();
    
    if (avg >= 90)
      grade = 'A';
    else
    if (avg >= 80)
      grade = 'B';
    else
    if (avg >= 70)
      grade = 'C';
    else
      grade = 'F';

    return grade;
  }

}

答案1

得分: 1

*public static void main(String[] args)* 方法是标准 Java 应用程序的入口点。如果您想要从命令行运行应用程序,您至少需要一个带有此方法的类。在一些特殊情况下,如果一个 JAR 是一个库或一个将以某种方式添加到另一个将使用它的应用程序的包,那么可以构建一个没有这种入口点的 JAR。但现在您不需要深入了解这些细节。

您看到的例子可能只是如此。这些示例缺少入口点,因为它们不是完整的应用程序。您的教授上传的代码只是一个类。它无法被执行。这只是展示一个类的方式。
英文:

The public static void main(String[] args) method is the entry point for a standard Java application. You need at least one class with this method if you want to run it from the command line. There are some special cases where you can build a JAR without such entrypoint if that JAR is a library or a package that will somehow added to another application that will use it. But you don't want to go into that detail now.

The examples you've seen are probably just that. Examples. And they lack the entrypoint because they are not complete applications. The code your professor uploaded is just a Class. It couldn't be executed. It is just a way of showing a class.

答案2

得分: 0

永远都必须有一个 main() 方法存在。它并不总是在你自己的代码中。如果你正在使用Java框架,main() 方法可能在框架代码中而不是你自己的代码中。你的代码中也可以有多个 main() 方法,但只有一个会在特定运行时作为程序的入口被使用。每次运行Java程序时,你实际上是在运行一个 main() 方法来启动程序。

英文:

There always has to be a main() method somewhere. It isn't always in your own code. If you're using a Java framework, the main() might be in the framework code rather than your own code. You can also have multiple main() methods in your code, but only one will ever be used as the entrypoint to your program for a particular run. Every time you run a Java program, you are running exactly one main() method to start the program.

答案3

得分: 0

如果您使用的是Java 9或更高版本,您可以使用主方法之外的另一种方法,即JShell。

这是一个交互式工具,允许您在终端/cmd中即时评估代码。
也被称为Read-Evaluate-Print Loop(REPL),它评估在输入时的声明、语句和表达式,并立即显示结果。

它在官方文档中有很好的解释:https://docs.oracle.com/javase/9/jshell/introduction-jshell.htm

每当我想测试或探索新的Java功能时,我都发现它非常有用。

英文:

If your're using Java 9+, you have an alternative from the main method which is JShell.

This an interactive tool which allows you to evaluate code in place using your terminal/cmd.
Also known as Read-Evaluate-Print Loop (REPL), evaluates declarations, statements, and expressions as they are entered and immediately shows the results.

It's well explained in oficial doc: https://docs.oracle.com/javase/9/jshell/introduction-jshell.htm

I find it usefull whenever I want to test or explore a new Java feature.

答案4

得分: 0

试图涵盖基本概念:

1) 一个JAVA程序可以包含任意数量的类,但最多只能声明一个类为public。

2) 如果有public类,则程序的名称和public类的名称必须匹配,否则会在编译时出现错误。

3) Main()方法与JAVA文件的名称之间没有关系。

当您编写没有main()方法的类时,它会编译通过。

但是当您运行代码时,会出现错误:
NoSuchMethodError:main

示例:

Test.java

class A(){
	public static void main(String args[]){
	System.out.println("在类Main中:A");
	}
}
class B(){
	public static void main(String args[]){
	System.out.println("在类Main中:B");
	}
}
class C(){
	public static void main(String args[]){
	System.out.println("在类Main中:C");
	}
class D(){
	}	

--------------------------------
Javac Test.java 
--> 没有编译时错误

Java A
输出:在类Main A

Java B
输出:在类Main B

Java C
输出:在类Main C

Java D
输出:NoSuchMethodError:main

Java Test
输出:NoClassdefFoundError:Test

--> 您会得到运行时错误。

当您创建JAR文件或任何构建的包时,不需要main()方法,因为您导入包或.Jar文件,并在工作类中调用这些方法()。

当您运行应用程序并希望自动获得输出时,public static void main(String[] args)方法是编译器查找程序入口点的签名。

您的教授上传的代码只是一个类。它会编译通过。
从上面的代码中,您可以在同一个文件中拥有许多main()方法。但是您必须运行特定的类。

尝试运行Student类:您将得到一个错误:在类Student中找不到main方法,请将主方法定义为:public static void main(String[] args)

英文:

Trying to Cover Basic Concept :

1) A JAVA program contain any number of classes, but at most one class can be declared as public.

2) If there is public class, then the name of program and the name of the public class must be matched other-wise we get compile time error.

3) There is no relation of Main() method & name of the JAVA file.

When you write class without main() method. It will compile fine.

But when you run the code it will give error :
NoSuchMethodError : main

Example :

Test.java

class A(){
	public static void main(String args[]){
	System.out.println("In class Main : A");
	}
}
class B(){
	public static void main(String args[]){
	System.out.println("In class Main : B");
	}
}
class C(){
	public static void main(String args[]){
	System.out.println("In class Main : C");
	}
class D(){
	}	

--------------------------------
Javac Test.java 
--> No Compile Time Error you will get

Java A
o/p : In class Main A

Java B
o/p : In class Main B

Java C
o/p : In class Main C

Java D
o/p : NoSuchMethodError : main

Java Test
o/p : NoClassdefFoundError : Test

--> Run Time error you will get.

When you create JAR file or any package built. There is no need of main() method because you import the package or .Jar file and you call those method() in the working class.

When you run the application and want to automatically get the output. The public static void main(String[] args) method is the signature where the compiler find the entry point of the program.

The code your professor uploaded is just a Class. It will compile fine.
From above code you can have a many main() methods in same file. But you have to run specific class.

Try to run Student class : you will get an Error: Main method not found in class Student, please define the main method as:
public static void main(String[] args)

huangapple
  • 本文由 发表于 2020年9月16日 11:11:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63912553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定