简体   繁体   中英

Java Static Method Calling

is Calling a static Java method ( a factory class method ) creates an object of that Class?

I mean a static method returns a value let's say an Array's size ( array is variable of class )

I've checked the code but couldn't see that the Object of that class never instantiated before calling the static method. ?

public static boolean isFiveInstance() {
    return _instances.size() == 5;
}

and _instances is class variable

private static ArrayList<LocalMediaPlayer> _instances;

and is being created and filled in the constructer.

No it does not. That is the point behind creating static methods. Static methods use no instance variables of any object of the class they are defined in either, so everything you refer to inside your static method must be static also.

That is why you call a static method like Class.StaticMethod() instead of:

new Class().StaticMethod();

the new will instantiate that class, thus creating a new instance of that object.

No, static invocations do not instantiated objects (because they do not require one).

The first time you refer to a class, including static method invocation, the class is loaded. by the classloader.

That's where the static initializer comes into play:

static {
     // do something
}

this block is called whenever the class is initialized (once per classloader)

No, calling a static method does not create an instance of the class. That's where static methods differ from instance methods. They don't need an instance of the class they belong to to be instantiated to be run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM