Open In App

Replacing ‘public’ with ‘private’ in “main” in Java

Last Updated : 21 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

Consider following Java program:




class GFG {
    public static void main(String args[])
    {
        System.out.println("GeeksforGeeks");
    }
}


GeeksforGeeks

Explanation:
1)public: It is an access specifier which allows the JVM(Java Virtual Machine) to access the main method from anywhere.
2)static: static keyword allows the JVM to access the main method without any instance(object).
3)void: It specifies that the main method doesn’t return anything.
4)main: name of the method(function) configured in JVM.
5)String args[]: Command line arguments.

Now, if we replace ‘public’ with ‘private’ in “public static void main”, the above code becomes:




class GFG {
    private static void main(String args[])
    {
        System.out.println("GeeksforGeeks");
    }
}


Explanation:
The above code will be compiled successfully, but will throw a runtime error as follows:

Error: Main method not found in class GFG, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

Click to view output

Reason: Since the access specifier was changed from “public” to “private” JVM was unable to access/locate the main method.


Previous Article
Next Article

Similar Reads

How to Solve java.lang.IllegalStateException in Java main Thread?
An unexpected, unwanted event that disturbed the normal flow of a program is called Exception. Most of the time exception is caused by our program and these are recoverable. Example: If our program requirement is to read data from the remote file locating in U.S.A. At runtime, if a remote file is not available then we will get RuntimeException sayi
5 min read
How to Access Private Field and Method Using Reflection in Java?
If we want to access Private Field and method using Reflection we just need to call setAccessible(true) on the field or method object which you want to access. Class.getDeclaredField(String fieldName) or Class.getDeclaredFields() can be used to get private fields. Whereas Class.getDeclaredMethod(String methodName, Class<?>... parameterTypes)
3 min read
In Java, Can we call the main() method of a class from another class?
In Java, Can we call the main() method of a class from another class? OR How to call 'public static void main(String[] args)' method from our code?These are some questions that usually puzzles a Java programmer. This article aims to provide an answer to these problems in a simple and efficient way.As we know, the main() method for any Java applicat
4 min read
Java Program to Print any Statement without Using the Main Method
As we know that the static block executes before the main method, hence we can place the statement that we want to execute in the static block, But in case of JDK7, and the above versions of the JDK, the code would not execute as compiler firstly looks for the main method before any other thing. Also, it depends on the IDE being used to run the pro
2 min read
Replacing Element in Java Vector
To replace an element in Java Vector, set() method of java.util.Vector class can be used. The set() method takes two parameters-the indexes of the element which has to be replaced and the new element. The index of a Vector is zero-based. So, to replace the first element, 0 should be the index passed as parameter. Declaration: public Object set(int
2 min read
Replacing All Occurrences of Specified Element of Java Vector
The replaceAll() method of java.util.Collections class is used to Replaces all occurrences of one specified value in a list with another. More formally, replaces with newVal each element e in list such that (oldVal==null ? e==null : oldVal.equals(e)). (This method has no effect on the size of the list.) Let's consider the following vector: [caption
3 min read
Replacing All Elements of Java Vector
All the elements of a vector can be replaced by a specific element using java. util. Collections. fill() method.The fill() method of java.util.Collections class is used to replace all of the elements of the specified list with the specified element. Let's consider the following vector:[caption width="800"]VECTOR BEFORE REPLACING[/caption]Let's say
2 min read
Replacing All Occurrences of Specified Element of Java ArrayList
ArrayList is present in java.util package and it is the implementation class of List interface. It does allow the duplicates elements and also it maintains the insertion order of the elements. It dynamically resized its capacity. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is
3 min read
Main App Implements Runnable | Concurrent Programming Approach 2
Prerequisite: Different Approaches to Concurrent Programming in Java Let's look at the second approach in detail. The user has the main class that implements runnable which is a promise to the compiler that the class will have a run method. public class MyClass implements Runnable{ public void run(){ } } The user then passes a reference to the main
3 min read
How to Convert java.sql.Date to java.util.Date in Java?
If we have the Date object of the SQL package, then we can easily convert it into an util Date object. We need to pass the getTime() method while creating the util Date object. java.util.Date utilDate = new java.util.Date(sqlDate.getTime()); It will give us util Date object. getTime() method Syntax: public long getTime() Parameters: The function do
1 min read
Article Tags :