Yes, we can declare the main method as private in Java. It compiles successfully without any errors but at the runtime, it says that the main method is not public Output:
class MainMethod {
private static void main(String args[]){
System.out.println("Welcome to Main Method);
}
}
Error: Main method not found in class MainMethod , please define the main
method as:
public static void main(String[] args)
Can we overload the main method in Java?
What is a Class/Static Variables?
What are the important features of Java 8?
What is Method Overloading in Java ?
How to create a table in Java with JDBC Connection?
What is the Difference between Path and ClassPath in java?
What is Instance variable?
What is the difference between JDK and JRE?
How to Create Objects in Java?
Can we declare the main () method as final in Java?
Convert given time in String format to seconds
Can we declare the main method as private in Java?
Can we declare a static variable within a method in java?
Can we define a static constructor in Java?
Memory Allocation in Java
How to check Java version?