answersLogoWhite

0

Why public is used in the main method?

Updated: 8/20/2019
User Avatar

Wiki User

11y ago

Best Answer

Because the compiler requires it. The main method has tohave the signature

public static void main(String[])

Otherwise the compiler will not recognize it as the main method (and will give a compile error).

Also, if the method wasn't public, then it would not be accessible outside of the class, so couldn't be called!

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why public is used in the main method?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is there any change by interchanging the position of main in Public static void main in java?

No. You can write it in as many ways as you want. The words public, static and void can be interchanged during the method declaration and still the main() method will continue to work in the same way. i.e., public static void main(String[] args) is the same as static public void main(String[] args) However, if you miss either of these 3 keywords from the method signature, the compiler will still let you compile the method, but it just won't be the main method that can be used to start the program execution.


How many types can write public static void main in java?

You can write it in as many ways as you want. The words public, static and void can be interchanged during the method declaration and still the main() method will continue to work in the same way. i.e., public static void main(String[] args) is the same as static public void main(String[] args) However, if you miss either of these 3 keywords from the method signature, the compiler will still let you compile the method, but it just won't be the main method that can be used to start the program execution.


What are the valid signatures of the main method of a class?

The main method can be declared as either of the below: public static void main(String[] args) or public static void main(String args[])


Why you use main method when you can use static method?

The main method is called by the jvm when your program is executed. The main method is a static method: public static void main(String[] args) {} A static method is method that can be run without instantiate the class (creating an object from it) The main method is a static method. No other static method could replace it's functionality. PS By static method do you mean static initialiser? I often use static initialisers instead of a main method, but in these cases you must still have a main method, albeit an empty one ie. public static void main(String[] args) {} Notice that the method has an empty body A main method must be used if you intend to accept parameters at run time from the jvm.


The main method of a program has?

The Java main method of a program is the place where the program execution begins. A main method would look like below public static void main(String[] args){ }


Does the order of public and static declaration matter in main method?

No


What is the main method of transportation in Sweden?

Car is a very common method of transportation.Cars, then public transportation.


What does public mean in java?

The keyword public is an access specifier. A variable or a method that is declared public is publicly accessible to any member of the project. Any class or method can freely access other public methods and variables of another class.


Void in java?

Void is a keyword in Java. It is always used in method declarations. When used in the line that declares a method, it signifies the fact that the method will not return any value. The JVM will not expect the method to return anything. Ex: public static void main(String[] args) {}


Can you overwrite main function in java?

You can not overwrite a static method in Java,and 'main' is a static method.so,you can't overwrite the 'main'.


What is the parameter specification for the public static void main method?

The Main method is the method in which execution to any java program begins. A main method declaration looks as follows: public static void main(String args[]){ } The method is public because it be accessible to the JVM to begin execution of the program. It is Static because it be available for execution without an object instance. you may know that you need an object instance to invoke any method. So you cannot begin execution of a class without its object if the main method was not static. It returns only a void because, once the main method execution is over, the program terminates. So there can be no data that can be returned by the Main method The last parameter is String args[]. This is used to signify that the user may opt to enter parameters to the java program at command line. We can use both String[] args or String args[]. The Java compiler would accept both forms.


What is the task of main method in a java program?

The main method is simply an entry point to your executable code. When you run a Java program, it looks for a main method as the first section of code to execute, which is why all of your programs start there.