answersLogoWhite

0

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

Updated: 8/19/2019
User Avatar

Wiki User

13y ago

Best Answer

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.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why you use main method when you can use static method?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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


How do you call a method from inside the main method?

I assume the question is about Java. How you call a method from inside the main method depends on whether this is an instance method or a static method. To call an instance method, you need to have a valid instance. Say foo is an instance of class Foo, which was created inside the main method. Then to call the instance method implode() use foo.implode() To call the static method bar() of class Foo, use Foo.bar().


Can the main method call the other method if it is not defined as static?

No


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[])


Which method is called first of all other methods when any application executed in c sharp?

static method Main() of the start object/class.There are 4 different signatures of Main():static void Main();static int Man();static void Main(string[] args);static int Main(string[] args);The starting object/class can have only one of them. Other classes or objects may have Main() method as well. (But why would be the question, Main() is NOT a good OO method name anyway. Method name should be a verb or start with a verb)


Is it compulsory to write static with void main?

Every C# application is started on the startup class' static Main method. Yes, it must be static, but void is not the only returned type. The 4 signatures:static void Main();static void Main(string[] arguments);static int Main();static int Main(string[] arguments);Ideally, there should be one and only one Main method in your application.


What is the use of static in public static void main?

The static keyword signifies that the method belongs to the class and not any of its objects. Hence, the JVM does not have to create an object of that class before beginning execution. Imagine how the JVM would create an object and invokes its method even before the class execution starts. That is why the main method is static and is the starting point of any java application.


Can main method be non-static?

No, the reason is simple: A method marked as a static means, that you don't need create an instance (create an object with the 'new' operator) in order to use it. The main method is the entry point for a java application, therefor there is nothing after you call it. no one who can create an object of the type of your class and call the main method. So the jvm must call the main method with no object reference.


what is the difference between final and static keywords?

static: we can use the keyword static either to method or to a variable. when we declare to a method,(eg: public static void main(String args[]),we can use this method without any object. when we use to a variable,there will be only one instance of that variable irrespective of how many objects that get created of that class. Final: Usage of final to method or to a variable makes them as constant. It's value cannot be changed...


Why main method is static?

Main method in java is always static as main method id the only method from where the program execution starts,and as we all know that main method is defined inside a class so JVM needs to make a object of the class to call the method and objects are build inside the main method ,so to execute the main method it has to make objects of the class but objects are build inside main method so that's the reason that main method is static so that JVM can execute the main method without making its object as static members can be called by class anme only


How do you invoke static methods?

if some method is static, then you can not call that method through the oobject of that class. but the name of the class. let us see a example: class Test { int a; int b; static void show() { System.out.println("we are in show"); } } class Main { public static void main(String args[]) { Test t=new Test(); t.show();\\thiss is an erroraneous code. because, the method "show()" is static. Test.show();\\this is correct } Arnas Sinha


Why using the name main for main method in java?

If you run an java file(as an .class or .jar file) there's always 1 method being called: The main(String[] args) method.The method is only called once.Example of an main method:public static void main(String args[]) throws IOException {LoggingBootstrap.bootstrap();gui = new GUI();}In this case it only bootstraps the logger and uses the constuctor method of GUI(the graphical user interface of the program)