Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an instance of a class automatically created when you first call a static method

I would like to know if you have a class with only static methods in it, does an actual instance of the class get created somewhere when you call 1st static method?

This is somewhat confusing to understand in terms of memory management as you never actually call the constructor or create an instance of the method explicitly.

If an instance does get created, I would like to better understand where this instance lives and for how long.

like image 604
AlexVPerl Avatar asked May 27 '15 01:05

AlexVPerl


People also ask

Do you need an instance of a class to call a static method?

A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables.

Are static methods called automatically?

A static method has no self argument and it is nested in a class and is designed to work on class attributes instead of instance attributes. Static methods never receive an automatic self argument, whether called through a class or an instance.

Can an instance call a static method?

Instance method can access static variables and static methods directly. Static methods can access the static variables and static methods directly. Static methods can't access instance methods and instance variables directly. They must use reference to object.

When a new instance of a class is created which method is called?

A function like Turtle or Point that creates a new object instance is called a constructor, and every class automatically provides a constructor function which is named the same as the class.


2 Answers

No. Calling a static method does not require (or create) an instance of a class. See also JLS-8.4.3.2 static methods which says (in part)

A method that is declared static is called a class method.

...

A class method is always invoked without reference to a particular object.

like image 170
Elliott Frisch Avatar answered Oct 20 '22 05:10

Elliott Frisch


Say you have

static class Foo
{
    static Bar bar = new Bar();

    static int func(){ ... }
}

obviously, a Foo object will not be created for calling func().

However, the class Foo needs to be loaded in memory; and to the application, there is an object corresponding to the class, which can be referred to as Foo.class, or Class.forName("Foo").

A loaded class is not initialized yet. When you call a static method for the 1st time, the class is initialized; some "space" is allocated for static variables, and static initializer code (like new Bar()) is executed.

This "space" is not visible to application as an object; but it's an in memory data structure too that concerns garbage collections (and other objects it refers to, like bar)

The class, and the "space", are only eligible for GC when the classloader that loaded the class is eligible for GC. For usual command line applications, that never happens. But for a lot of other applications, class GC is important, and class loading needs to be carefully done.

like image 22
ZhongYu Avatar answered Oct 20 '22 05:10

ZhongYu