answersLogoWhite

0

Can main method be non-static

Updated: 8/9/2023
User Avatar

Wiki User

14y ago

Best Answer

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.

User Avatar

Wiki User

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

Wiki User

12y ago

We can able to call the not static methods from static main() by creating the object of that class.

For Example:

public class AbstractDemo1 {

void display()

{

System.out.println("HI");

}

public static void main(String[] args) {

AbstractDemo1 a = new AbstractDemo1();

a.display();

}

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can main method be non-static
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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


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){ }


What is a main function in java?

the main function or main method is the first method that gets invoked when you try to run a java application. Any class that has a main method can be executed in a standalone fashion. A typical main method would look like below: public static void main(String[] args) { … // code goes here }


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


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.

Related questions

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.


What is void main?

The main method is the first method, which the Java Virtual Machine executes. When you execute a class with the Java interpreter, the runtime system starts by calling the class's main() method. Themain() method then calls all the other methods required to run your application. It can be said that the main method is the entry point in the Java program and java program can't run without this method.The signature of main() method looks like this:public static void main(String args[])The method signature for the main() method contains three modifiers:public indicates that the main() method can be called by any object.static indicates that the main() method is a class method.void indicates that the main() method has no return value.


What is the main purpose of the scientific method?

The main purpose of the scientific method is to test ideas


How nonstatic fields can be used in public static void main of a class?

They can't be. If you want to use a non-static field in a static method, you need an instance of the class to reference. class MyClass { int myX; static int staticX; public static void main(String[] args) { // We can directly reference staticX whenever we want. staticX = 10; // We need an instance of MyClass to use myX. MyClass mc = new MyClass(); mc.myX = 10; } }


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


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){ }


If application can be run without main what need of main method?

Well, if an application could be run without a main method, then you would not need it. However, Java applications cannot run if they do not contain a main method.


Can main method be overloaded illustrate with example program?

Yes, you can very well overload the main method but, the main method that would get invoked by the JVM when you try to run this class would be the main method that has the below signature: public static void main(String[] args) { ... } All other main methods would be accepted by the Compiler but only the above method would get called when you run the class


Are you allowed to declared main method in every class?

Yes. Every Java class can have a main method


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

No


What is a main function in java?

the main function or main method is the first method that gets invoked when you try to run a java application. Any class that has a main method can be executed in a standalone fashion. A typical main method would look like below: public static void main(String[] args) { … // code goes here }


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