1

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.

asked Jul 21, 2011 at 5:38

3 Answers 3

4

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.

answered Jul 21, 2011 at 5:42
Sign up to request clarification or add additional context in comments.

Comments

3

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)

answered Jul 21, 2011 at 5:42

4 Comments

so ? when I call a static method of the class by Class.staticMEthod(); this static block is also invoketed, right ? where this static block is in the constructer.
the staic block is invoked the first time you refer to the class. And it is placed outside the constructor
ok, I think I got the point. first, you are right it is out of the constructor ( my mistake ). And since I am referring the class by calling the static method like Class.StaticMethod(); it is executing the static block as well. Even if I dont have a Class instance created.
Here is a link for those who want to check more in details. External Page
2

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.

answered Jul 21, 2011 at 5:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.