Java main() method

Let’s see the following simple Java code :

class Test
{
}



Now we will save as Test.java, then we will compile(javac Test.java), then it will happily compiled. Then we will execute(java.Test), but it will not execute when we will execute we will get the following message:


Here I observed that whether class contains main() method or not and whether main() method is declared according to the requirement or not, that is not checking by the compiler. So at run-time, JVM is responsible to check these things. When at run-time JVM is not able to find the required main() method, then we will get above run-time error message. Because JVM will first search where is main() method and that should be public static void main(String[] args).

Now we will write the program as:
Test.java

class Test
{
   public static void main (String[] args)
   {
       System.out.println("Welcome 2 java8s.com");
   }
}

This simple program identifies some silent features of the language. Lets discuss line by line and understand the unique features that constitute a java program.

  • The first line identifies the class declaration.

class Test declares a class. We know that java is an OOP language, therefore everything must be placed inside the class. Test is a java identifier that specifies the name of the class to be defined. We know, every class definition begins with an opening brace "{" and ends with a matching closing brace "}" appearing in the last line in the example.

  • The third line identifies the main method that is:
  • public static void main (String[] args)
  • First it defines a method main
  • Conceptually this is similar to the main( )function in C/C++.
  • Every java application program must include the main () method and this is the starting point for the interpreter to begin the execution of the program.
  • A java application can have any no. of classes but only one of them must include a main method to initiate the execution.

This line consists a number of keywords such as publicstaticVoid

public:The keyword public is a modifier which declare that the main() method can be accessed by any number of classes, and another point is JVM can call main() method from anywhere.

static:The keyword static declares that only one copy of the main() method is created for whole class. In OOP, generally method is invoking by object, but main() is invoking without any existing object, JVM is calling this main() method directly, because it is a class level concept.

void:The return type void represents that the main() method never returns any value.

The main method takes a parameters i.e String[] args. Which declares a parameter named args, which contain an array of objects of the class type string. In this case args receives any command line arguments present when the program is executed.

  • The fifth line represent an output line i.e.

System. out. println ("Welcome 2 java8s.com");
This is similar to the print( ) statement of C or cout<< construct of C++. The println( ) is a method which is a member of the object out, which is a static data member of class system. This statement prints the string .

Welcome 2 java8s.com on the screen.
Here System is a class present in java.lang package, out is a static variable present in System class of type PrintStream, and println() is a method present in PrintStream class.

 

class System
{
    static PrintStream out;
}

System.out.println("Welcome 2 JavaRace");
Let's see the varieties of main() method declaration:
public static void main( String args[])
static public void main(String[] args)
final public static void main(String []args)
final synchronized static public void main(String args[])
synchronized final strictfp static public void main(String...args) 



Note : Here we can modify the name of the array that is args, that means instead of args we can give any name, because args is purely user-defined. For example,
public static void main(String[] demo)

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment