1

I am having a simple query.Basically its a theoritical question.I need to clear this idea.I have not found any suitable answer to it.The question is very simple.Suppose we have a class named as A

class A{
    ......
}

Now from my main function i am creating one object of A.

A obj = new A();

now after creating the object i will be able to access any methods,variables present in the class.

But for static methods or variables we can achieve this directly by using class name like

A.methodname();

now my question is if we are creating one object of any class then memory will be allocated for this.Now if we are using static methods are directly calling them via class name then we are not creating any objects,so in that case memory should not be allocated.But without allocating the memory how does we access the methods or any variable name?Using reference also require some memory allocation.so please explain how in this case memory allocation is happening or how we are accessing the methods or variables in the class.

1
  • To add to the already existing (and useful) answers, you will use the memory needed to pass the parameters to the function (in the stack, not in the heap). Commented May 13, 2013 at 11:50

6 Answers 6

2

Now if we are using static methods are directly calling them via class name then we are not creating any objects,so in that case memory should not be allocated.

Actually, you do. It is JVM dependent but the HotSpot JVM creates a special object for all static fields. You can see this object in a heap dump.

Making this an object makes it easier for the GC to trace which objects are being used. This class object is discarded when the ClassLoader is unloaded.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer.I have cleared my doubts.i did not know this " It is JVM dependent but the HotSpot JVM creates a special object for all static fields. You can see this object in a heap dump."..Thanks a lot Peter.Its a priceless info for me
can u give me little bit info regarding what is Hotspot JVM ?
The best wiki for HotSpot internals is here wiki.openjdk.java.net/display/HotSpot/Runtime
1

This is done by class loading.

We do not have any instance of object, but we have a class code loaded into memory so in fact there is memory allocation.

Comments

1

Static functions can only access static variables, and the memory for that will already have been allocated.

Comments

1

Static members (methods and variables) are allocated by the JVM when the class is loaded.

without allocating the memory how does we access the methods or any variable name?

You cant access an instance method or variable from an static method. It wont compile if you try.

From an static method, you can only access other static methods or static variables, (wich are allocated at class load time).

Comments

0

All the static variables of a class are loaded into memory at the time of class loading where as the instance variables are allocated with memory everytime you create a new object of that class.

However, the static variable values are not specific to a instance of an object but there are shared by all the objects of a class where as the instance variable values are specific to an instance(class object).

That is the reason you can access the static variable using class name as they are already loaded into the memory where as the instance variables and methods need object creating to access them.

Cheers!

Comments

0

For static during a class loading a memory allocation is done in permanent generation.Its initialization occurs only once .It is related to class not object.

Comments

Your Answer

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.