Replacing ‘public’ with ‘private’ in “main” in Java

Consider following Java program:

filter_none

edit
close

play_arrow

link
brightness_4
code

class GFG {
    public static void main(String args[])
    {
        System.out.println("GeeksforGeeks");
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

class GFG {
    private static void main(String args[])
    {
        System.out.println("GeeksforGeeks");
    }
}

chevron_right


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

Click to view output

Reason: Since the access specifier was changed from “public” to “private” JVM was unable to access/locate the main method.



My Personal Notes arrow_drop_up

A keen and enthusiastic learnerfull stack webdeveloper fascinated by data structure and algorithms

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.