TheLostBoys
TheLostBoys

Reputation: 33

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

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;
  }

}

Upvotes: 3

Views: 3961

Answers (6)

Basil Bourque
Basil Bourque

Reputation: 340230

Implicitly Declared Classes and Instance Main Methods

As part of paving the on-ramp for people learning Java, you can preview a new feature in Java 22, and again in Java 23, to shorten the declaration of main:

class HelloWorld {
    void main() {
        System.out.println("Hello, World!");
    }
}

And you can even eliminate the declaration of its containing class. Here is an example of an entire .java file:

void main() {
    System.out.println("Hello, World!");
}

For Java 23, see JEP 477: Implicitly Declared Classes and Instance Main Methods (Third Preview)

For Java 22, see JEP 463: Implicitly Declared Classes and Instance Main Methods (Second Preview).

Upvotes: 2

Basil Bourque
Basil Bourque

Reputation: 340230

JavaFX

Special accommodation for JavaFX was made in Java 9+ to make optional the main method.

The java launcher in JDKs based on OpenJDK source code can directly launch a JavaFX application JAR. The app developer may opt to include a main, but not required.

See: JEP 153: Launch JavaFX Applications.

Upvotes: 0

Vishal Sheth
Vishal Sheth

Reputation: 160

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)

Upvotes: 0

Ionut S.
Ionut S.

Reputation: 21

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.

Upvotes: 0

drkblog
drkblog

Reputation: 398

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.

Upvotes: 2

CryptoFool
CryptoFool

Reputation: 23139

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.

Upvotes: 0

Related Questions