Rose
Rose

Reputation: 361

Why we do not need to create object for static method?

I know why main method is static. If it is static there is no need to instantiate that class by JVM. Without object we can call it. But why object is not needed to call static method?

Can anyone explain please?

Upvotes: 6

Views: 13454

Answers (9)

Yogesh Bajgude
Yogesh Bajgude

Reputation: 11

Because static method belong to class and it's not an object specific. Where as for non static / instance method we need to create an object.

Upvotes: 1

runzhi xiao
runzhi xiao

Reputation: 184

Because it belongs to the class object which you do not need to create manually.

Upvotes: 0

user2512425
user2512425

Reputation: 1

When we execute a java file, a java compiler loads and executes the static member automatically.

I'm new in java so please forgive me if my answer is wrong.

Upvotes: 0

queueoverflow
queueoverflow

Reputation: 1

Yes you are correct that you don't need an instance object to call the static method of a class, because static methods belongs to a class, and not to the instance of that class. Also, you cannot use instance variables inside the static method because instance variables belong to the instance.

Upvotes: 0

venki
venki

Reputation: 1131

Consider this example, there is a family containing a mother and three children. Mother brings three ice cream cones to each of the children, but brings only one PSP for all the three children. All children use the same PSP but they have their own ice creams.

Here ice cream is a not-static thing (method/variable), PSP is the static thing, Mother is the class, children are objects.

It's pretty simple. Static belongs to a class, it is common for all the objects of a class. Not-static things are object specific.

Upvotes: 6

Linga
Linga

Reputation: 10553

The main() method is static because they can then be invoked by the runtime engine without having to instantiate an instance of the parent class.

Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class

Upvotes: 0

pickypg
pickypg

Reputation: 22332

Because the JVM can call the method for you (however it sees fit). Otherwise, where would the rabbit hole end? They could have done exactly what you're suggesting by creating a known interface, itself with a main method. For example:

interface ApplicationStarter
{
    void start(String []args);
}

But then there concerns related to the constructor. Numerous frameworks exist that run into similar problems, such as SPI, which requires a default (no-arg) constructor for similar reasons. Such frameworks fail when their pre-known requirements (e.g., no-arg constructor or perhaps not Serializeable for some other frameworks), and beginners find this hard. Making the most basic part of an application "complicated" is not a good way to achieve adoption.

For an application starting/entry point, it's far easier to depend on a known entry point (main) that is analogous to practically every other language: no worries about the object not constructing, or overriding.

Upvotes: 0

digitaljoel
digitaljoel

Reputation: 26574

A static method is associated with the class, not with any instance of the class.

See http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Upvotes: 7

Ankit HTech
Ankit HTech

Reputation: 1893

Object is needed for the member variables and methods but static is the application variable or function this one of the reason why object is not needed for the static.

Upvotes: 0

Related Questions