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.
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.
This line consists a number of keywords such as public, static, Void
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.
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)
Again for better understanding, follow this video:
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc