- 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
Replacing ‘public’ with ‘private’ in “main” in Java
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.
Advertisements