answersLogoWhite

0


Best Answer

They are public because they must be accessible to the JVM to begin execution of the program. Main is the first method that would get executed in any class.

They are static because they must 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.

Or

Main is starting point of the program execution Main method is called by JVM there is no need for JVM to

create an object of the class just to access Main function

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

The main method always has to be public, or else it will never run. If you mark the method as private and you try to run the application, the class loader won't even be able to see the method, and won't get called. The main method needs to be static so your class does not need to be instantiated in order to run. When you run your application, the JVM can not create an instance of the class. That can only be done once the main method is called. Methods of classes that can be called without the class being instantiated are static.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

in java programming langauge the main function is declear as static and public because in oops(object oriented programming) every public function is accessable any where in our source code , but the matter of static is different from this in a class every static function is called without object so we do not declear a object for our class in our code

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why main function written as public static in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

In java public static voidmain function denotes what?

Java's main function denotes the entry point into the execution of your program.


Can you interchange the term public and static in java?

The two are different, and independent from one another. A variable can be public, static, both public and static, or neither.


What is psvm in java?

public static void main


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'.


Why the main function is public and static?

The main function is public because it means it needs to be called by any object. I had to check this myself but if you try to make the main function in Java private it will give you a run time error. It is static because it is indicating it is a class method and it does not need to be instantiated. Without the static keyword you would need to do something like this Main main = new Main();


What is public static final variable in java?

-Public to all - Static to access with Class Name - Final to change( constant and not alowed to change) Just use it along with class name. (As implied above, a 'public static final' variable in Java is what other languages would call a Constant. )


What is the difference between method and function in java?

A function is a piece of code that can be reused by calling its name while a method is a function that is associated with a class. In Java, functions are usually referred to as static methods.


What is the name of the function which must be defined in a Java program?

The main function. Every program must have a main function and it must be declared static.


What is the error in exception handling in java with example?

class Demo { public static void main(String s[]) { System.out.println("hello java frm Demo"); } } class Demo1 { public static void main(String s[]) { System.out.println("hello java frm Demo1"); String z={" "}; Demo.main(); } }


Static membors partispating Overwriting in java?

Static membors partispating in Overwriting in java?


How to Write a java program to display hello?

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


Sample code in java programming?

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