public static void main(String args) | Java main method Onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
The main method is the entry point of any core Java program. Here, I mention the core Java program specifically because, in all the other java programs like Servlets, applets and any Java-based frameworks, they have their own life cycles and own entry points.
For example in Servlet programming, we don’t have any main method, but it does not mean that there is no entry point for a servlet. The starting point of a servlet is init().
Java main method is called by the main Thread, which is created by the Java Virtual Machine(JVM). And the program containing the main method runs until the main thread is alive. After completion of the main thread, the program will be terminated.
java class name 
When we hit the above (class name followed by the “java”) command, the JVM checks for the main method with the default prototype.
Default Syntax for main method:
public static void main(String args[])
The main method should be public. The public keyword is the access modifier in java. By making the main method as public, the method can get access from anywhere, any package. For instance, consider in your system JVM is installed under the “C:\” directory but the java program containing the main method is in the “D:\” directory, by making the main method as public the JVM which is present in “C:\” will be allowed to get access to the main method in “D:\”.
Static is also one of the access modifiers in Java. When we declare a  variable, method, block, class with static modifier those are all under class level access, and not for a specific instance or object. Hence the static functionalities are directly accessed by the class name. For direct accessing with JVM, the main method should be static. Hence JVM will call the main method directly by using the class only, without creating any instance of the main method class.
Whenever we declare a method with “int” or any return type except “void”, that means we can catch hold of that particular return value and make use of that value from the called method and do something on that value. But the main method is called by the JVM. JVM will not expect any value from the main method as a return value because there is no more functionality done by the JVM after getting the return value from the main, hence the main method is declared as “void”.
In JVM the main method name is configured as a main. It is not necessary to keep the main method name always as main. If we change the configuration of JVM with respect to the main method, then we can change the name of the main method as we want.
Java main method takes the array of Strings as a parameter. By using this parameter we can pass the command line arguments to the main.

public static void main(String args[])
public static void main(String[] args)
public static void main(String []args)
public static void main(String... args)
static public void main(String args[])
public static void main(int[] args)

public static void main(String args[])
public static void main(String[] args)
public static void main(String []args)
public static void main(String... args)
static public void main(String args[])
public static void main(int[] args)

We can make the main method as final.
We can make the main method synchronize.
We can make the main method as strictfp (strict floating point).
We can overload the main method.
We can also override the main method but it is termed as data hiding (not overriding).

We can make the main method as final.
We can make the main method synchronize.
We can make the main method as strictfp (strict floating point).
We can overload the main method.
We can also override the main method but it is termed as data hiding (not overriding).
Hence the below syntax is applicable for the main method:
final static synchronize strictfp public void main(String args[]){}
We can overload the main method, and also we can call the main method if need.
Example:
public class Parent {

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

    /**
     * Overloading main method
     */
    public static void main(int[] args) {
        System.out.println("int args::" + args[0] + "" + args[1] + "" + args[2]);
    }
}

class Child {

    public static void main(String[] args) {
        int[] intArgs = {1, 2, 3};
        Parent.main(intArgs);
    }
}
By running the Child class we get the below output.
Output:
int args::123
If the main method is overridden it is termed as Method Hiding. We can hide the main method as shown below.
Example:
public class Parent {

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

}

class Child extends Parent {

    public static void main(String[] args) {
        System.out.println("In Child main");
    }
}
By running the Child class we get the below output.
Output:
In Child main
Happy Learning 🙂

Java Static Variable Method Block Class Example
Overriding vs Overloading in Java
Java String to int conversion Example
Java Reflection Get Method Information
Top 10 Exceptions in Java
How to Split String in Java example
Default Static methods in Interface Java 8
Factory Method Pattern in Java
String sorting in Java
How to check whether a String is a Balanced String or not ?
How to Convert Java Int to String
Java String Binary Search Example
Java instanceof Operator
How to Convert Java String to Int
String in Switch in Java 7 Example

Java Static Variable Method Block Class Example
Overriding vs Overloading in Java
Java String to int conversion Example
Java Reflection Get Method Information
Top 10 Exceptions in Java
How to Split String in Java example
Default Static methods in Interface Java 8
Factory Method Pattern in Java
String sorting in Java
How to check whether a String is a Balanced String or not ?
How to Convert Java Int to String
Java String Binary Search Example
Java instanceof Operator
How to Convert Java String to Int
String in Switch in Java 7 Example






rajesh chaganti
May 13, 2015 at 5:57 pm - Reply 

Can you please explain about why main method declared as static as little eloborate..... please.....










rajesh chaganti
May 13, 2015 at 5:57 pm - Reply 

Can you please explain about why main method declared as static as little eloborate..... please.....




Can you please explain about why main method declared as static as little eloborate..... please.....
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String