Reputation: 2579
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.
Upvotes: 1
Views: 2173
Reputation: 597076
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)
Upvotes: 3
Reputation: 43219
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.
Upvotes: 2
Reputation: 15390
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.
Upvotes: 4