- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we declare main() method as private or protected or with no access modifier in java?
Java provides various access specifiers namely private, public and protected etc...
The Private modifier restricts the access of members from outside the class. A class and interface cannot be public.
The Public access modifier can be associated with class, method, constructor, interface, etc. public can be accessed from any other class.
The protected access modifier can be associated with variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
The default access modifier does not have keyword a variable or method declared without any access control modifier is available to any other class in the same package.
Therefore, if you declare a method public it can be accessed from anywhere outside the class. As we know that JVM accesses/invokes the main method directly if the main method is public JVM can invoke it from anywhere.
Declaring the main method private or, protected
You can define the main method in your program without private, protected or, default (none) modifier, the program gets compiled without compilation errors.
But, at the time of execution JVM does not consider this as the entry point of the program. It searches for the main method which is public, static, with return type void, and a String array as an argument.
public static int main(String[] args){ }
If such a method is not found, a run time error is generated.
Example
In the following Java program, the class Sample contains the main method which is public.
public class Sample{ private static void main(String[] args){ System.out.println("This is a sample program"); } }
Output
On executing, this program generates the following error.
Error: Main method not found in class Sample, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
- Related Articles
- Can we declare interface members as private or protected in java8?
- Can we declare an abstract method, private, protected, public or default in java?
- Can we declare a top level class as protected or private in Java?\n
- Can we declare a main method as private in Java?\n
- protected access modifier in Java
- Can we declare the main () method as final in Java?
- Can We declare main() method as Non-Static in java?
- private access modifier in Java
- Can we declare the variables of a Java interface private and protected?
- Can we declare a constructor as private in Java?
- Demonstrate Private access modifier in Java
- Why Protected access modifier used in Java?
- Can we override a private or static method in Java
- Can we declare an abstract method final or static in java?
- Can we have a private method or private static method in an interface in Java 9?\n
