
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Can we declare a main method as private in Java?
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.
Example:
class PrivateMainMethod { private static void main(String args[]){ System.out.println("Welcome to Tutorials Point"); } }
The above code is working successfully at compile time but it will throw an error at the runtime.
Output:
Error: Main method not found in class PrivateMainMethod, 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 main() method as private or protected or with no access modifier in java?
- Can we declare the main () method as final in Java?
- Can We declare main() method as Non-Static in java?
- Can we declare a constructor as private in Java?
- Can we declare a top level class as protected or private in Java?\n
- Can we declare an abstract method, private, protected, public or default in java?
- Can we have a private method or private static method in an interface in Java 9?\n
- Can we declare interface members as private or protected in java8?
- Can we execute a java program without a main method?\n
- Can we declare the variables of a Java interface private and protected?
- Can we override private methods in Java\n
- Can we overload Java main method?
- Can we declare constructor as final in java?
- Can we override a private or static method in Java
- Can we overload the main method in Java?

Advertisements