When ‘public’ is used in ‘main’ −
Example
public class Demo{ public static void main(String args[]){ System.out.println("This is a sample only"); } }
Output
This is a sample only
A class named Demo contains the main function that is public. It has a print function, which successfully compiles, executes and prints the message on the console.
When ‘public’ is replaced with ‘private’
Example
public class Demo{ private static void main(String args[]){ System.out.println("This is a sample only"); } }
Output
Error: Main method not found in class Demo, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
A class named Demo contains the main function that is private instead of being public. It has a print function, which doesn’t successfully compile, hence giving an error stating that the ‘main’ method was not found, since it couldn’t be accessed because of it being private.