Skip to content

Java main method | Syntax & Signature | Static

  • by

Java’s main method is the most important method in Java application or program. When you started the first program, you used the main method in code. Do check again the First Java Program – “Hello World”.

You can write an in the main method or any other method? Anything which you want to output from a computer. Like Loops – (for loop, while loop), statements and branching (if-else), etc. It up to you and your application requirement, it’s like a normal method with special characteristics.

Java main method Syntax & Signature Static

Syntax

Here is the syntax of exactly a main method looks like.

public static void main(String[] args) {
    // Your code and statments 
 }

Every Java application has at least one class and the main method. Java virtual machine run code between a curly { } of your program or application.

Method Signatures

Even in the single and very simple program have many in java main method. Let’s see the java main method signature used in keywords, methods and access modifiers, etc. We are starting one by one from top to bottom demonstration of the java program.

  • publicAccess modifier.
  • static – A used a type of method, no need to create a class object.
  • void – Return type (void does not return any value).
  • main – Name of the method, JVM looking for it to execute a program.
  • String[] args – A method parameters.

 Let’s go in depth about every signature has used a main() function.

#public

It’s access modifier, which gives him access to anywhere (global visibility) in program or application.

#static

The main method has to be static so that JVM can load the class into memory and call the main method. 

#void

A return type, like some methods, has return integer, string etc. but this method returns nothing so its type is void.

#main

A method name that is required to JVM identifies a program starting point.  Without it, the program will throw an error.

 #String[] args

It’s a method signature argument. This is also called java command line arguments.

This Topic is very important for a fresher and college student at Internal or external or company interviews.

Question: Why the main method is needed in Java?

Answer: What is the purpose of the main method in java?

Question: Does the main method compulsory in n Java?

Answer: In earlier versions of Java 5 it’s not mandatory but From JDK 6 (7, 8 so on) the main method is mandatory. 

If not used the main method then you will get an error –

Error: Main method not found in class

Question: The main method in Java belongs to which class?

Answer:  It belongs to the class where you define it. But any class can have a static main method.

One more point you don’t need the main method in every class, just one method to start the program is sufficient.

Question: Why java main method is static?

Answer: Because JVM can call a static method without creating an instance. and The Main method is static because of preventing data ambiguity.

Question: Is there only one way to Write a java main() method?

Answer: No, you can write Different Ways to Write a main() Method. Here is an example of different ways. But when you overload a method in java that time JVM only executes a Main() method with the exact signature. Here is most of the way to change a java main method args (arguments).

First – []args

public static void main(String []args) { }

Second – args[]

public static void main(String args[]) { }

Third –  Represented as varargs.

public static void main(String...args) { }

Fourth – Synchronized

public static synchronized void main(String... args)

Fifth – strictfp, compatibility between processors and floating point values.

public static strictfp void main(String... args)

Sixth – final, prevent the array from being modified

public static final void main(String... args)

Question: Can we Override and overload the Java main() method?

Answer: No, you can’t override a java main() method because its static methods and static methods can’t be overridden

And Yes, you can overload a java main() method, as many times you want but JVM always calls the original main method.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading