Java main method not public

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.
Читайте также:  Replace class with javascript

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.

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

Источник

Why is main method public, static, and void in Java? Answer

What is the main method in Java?
the main method in Java is a standard method that is used by JVM to start the execution of any Java program. the main method is referred to as the entry point of Java application which is true in the case of core java applications but in the case of container-managed environments like Servlet , EJB, or MIDlet this is not true as these Java programs have their own life-cycle methods like init() , service() or destroy() for Servlet’s which is used by the container. The main method in Java is run by the main thread which is a non-daemon thread and the Java program runs until the main method finishes or any other user thread is running.

When we start JVM by running the java command we also provide the name of the class that contains the main method, which is later invoked by JVM to start Java program execution. for example in the below command :

C:\Documents and Settings\JavaTutorial>edit Calculator.java code of Calculator class public class Calculator > C:\DOCUME~1\JavaTutorial>javac Calculator.java C:\DOCUME~1\JavaTutorial>java Calculator I am main class which contains the main method

Calculator class must contain public static void main(String args[]) method. Now if we change the signature of the main method and try to run the same Java program we will get an error as shown below :

public class Calculator< public static void main(String args)< System.out.println("I am main class which contains the main method"); > > C:\DOCUME~1\sharma>javac Calculator.java C:\DOCUME~1\sharma>java Calculator Exception in thread "main" java.lang.NoSuchMethodError: main

because we have changed String[] to String which means JVM not able to find a standard main method that is required to start execution of the Java program. You can learn more about the main method by joining these free Java Programming courses online.

Valid Signature of the main method in Java

the main method is a standard method and has a pre-specified signature if you change the signature of the main method JVM will not be able to locate the main method will throw Exception at runtime as shown in the above example.

The main method is public, static, and void and accepts a String[] as an argument and from Java 5 onwards it can also accept variable arguments instead of arrays. following signatures are valid main method signatures in Java :

public static void main(String args[]) <> public static void main(String[] args)<> public static void main(String. args)<>

You can also use certain modifiers like final , synchronized, and strictfp along with the main method in Java.

Why the main method is public static and void in Java

This is a very famous core Java interview question and appeared on many fresher and experience level interviews. Though every java programmer uses the main method not everyone is familiar with the reason why the main is static or why the main is public in Java.

1. The main method in Java is public so that it’s visible to every other class, even which are not part of its package. if it’s not public JVM classes might not able to access it.

2. The main method is static in Java so that it can be called without creating any instance. While JVM tries to execute the Java programs it doesn’t know how to create instances of the main class as there is no standard constructor is defined for the main class.

Difference between C, C++, and Java main method

If you come from C and C++ programming language then you know what is the main method as both of these programs also use main() as an entry point for program execution but the main method in C and C++ is quite different than the main method in Java.

The main method in java doesn’t return anything and has return type void while the main method is C and ++ return int.

That’s all on what is the main method in Java, What are the valid signature of the main method in Java, What happens if you put the incorrect signature of the main method, Why the main method is public, static, and void in Java and finally we saw the difference between C, C++ and Java main methods.

This is one of the most important fundamentals of the Java programming language and every Java programmer should be familiar with it.

Источник

Java main method Explained [Easy Examples]

The java main method is a standard method that is used by JVM to start the execution of any Java program. The java main method is referred to as the entry point of the Java application. In this tutorial, we will learn about the java main method. We will see how we can create the java main method and explain its every component in detail.

We will also discuss how we can pass different arguments to the java main methods by taking various examples. Moreover, we will also learn the overloading of the java main method. All in all, this is going to be one of the most informative tutorials about the java main method.

Getting started with Java main method

As we already discussed the java main method is the entry point for any Java application. The main method will subsequently invoke all the other methods required by the program. In this section, we will discuss the syntax and different ways of writing the java main method and will explain each of its components in more detail.

Different Java main method syntax

The java main method can be written in many ways depending on the situation. Although they’re not very commonly used, they’re valid signatures. Note that none of those are specific to the main method, they can be used with any Java method but they are also a valid part of the main method.

Syntax-1 to use java main method

Let us see some of those methods here. See the simple syntax of writing the java main method.

public static void main(String[] args) < // statements >

Syntax-2 to use java main method

The above one is the most commonly used syntax of the java main method. Notice that the square brackets are just after the String but they can also be written after args as well. See the syntax below:

public static void main(String args[]) < // statements >

This is a valid declaration of the java main method.

Syntax-3 to use java main method

Another way of writing the main method is to add strictfp with it, which is used for compatibility between processors when working with floating-point values. See the simple syntax below:

public static strictfp void main(String[] args) < // statements >

Syntax-4 to use java main method

Another way to write the java main method is to write the final inside the main method which can be applied on args to prevent the array from being modified. See the simple syntax below:

public static void main(final String[] args) < // statements >

Notice that the above all are valid but not commonly used methods of declaration, the very first one is the most popular one and we will also use that syntax in the rest of our tutorial.

Different ways of using Java main method

Understanding the “public” keyword

The public is a Java keyword that declares a member’s access as public. Public members are visible to all other classes. This means that any other class can access a public field or method. Further, other classes can modify public fields unless the field is declared as final. The main method in java is public so that it can be accessible everywhere and to every object which may desire to use it for launching the application.

Notice that if we will not make the main method public, then there will be no compilation error. But we will get runtime error because matching the main method is not present. Remember that the whole syntax should match to execute the java main method. For example, see the example below:

java main method

Notice that it says the main.java is not executable, that is because our main method is not public.

Understanding the keyword “static”

In the Java programming language, the keyword static indicates that the particular member belongs to a type itself, rather than to an instance of that type. This means that only one instance of that static member is created which is shared across all instances of the class. In simple words, static methods are the method that invokes without creating the objects, so we do not need any object to call the main method. If we will not make the main method static we will get the following error. See the example below:

java main method error

Notice that it says the main method is not found and gave us the proper syntax of the main method.

Understanding the keyword “void”

The void keyword specifies that a method should not have a return value. In java, every method should have a return type otherwise we will get an error. The main method’s type is void which means that it is not returning anything. If we create the main method without any return type (void) we will get the following error.

java main method void

Understanding the “string arg[]” in the main method

The java main method can also accept some data from the user. It accepts a group of strings, which is called a string array. It is used to hold the command line arguments in the form of string values. The agrs[] is actually an array name, and it is of String type. It means that it can store a group of strings. Remember, this array can also store a group of numbers but in the form of a string only. Values passed to the main method are called arguments. These arguments are stored into args[] array, so the name args[] is generally used for it.

Later in this tutorial, we will take examples and see how we can pass arguments to the main method.

Simple example of Java main method

Now we are familiar with the terms used in the java main method, we are good to go and create a main method and run it. See the example below which prints out a message.

Источник

Оцените статью