Consider following Java program:
class GFG { public static void main(String args[]) { System.out.println("GeeksforGeeks"); } }
Output:
GeeksforGeeks
Explanation:
1)public: It is an access specifier which allows the JVM(Java Virtual Machine) to access the main method from anywhere.
2)static: static keyword allows the JVM to access the main method without any instance(object).
3)void: It specifies that the main method doesn’t return anything.
4)main: name of the method(function) configured in JVM.
5)String args[]: Command line arguments.
Now, if we replace ‘public’ with ‘private’ in “public static void main”, the above code becomes:
class GFG { private static void main(String args[]) { System.out.println("GeeksforGeeks"); } }
Explanation:
The above code will be compiled successfully, but will throw a runtime error as follows:
Error: Main method not found in class GFG, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
Reason:
Since the access specifier was changed from “public” to “private” JVM was unable to access/locate the main method.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.