Reputation: 991
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.
Upvotes: 1
Views: 207
Reputation: 7651
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.
Upvotes: 0
Reputation: 137
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!
Upvotes: 0
Reputation: 1599
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).
Upvotes: 1
Reputation: 533790
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.
Upvotes: 2
Reputation: 7846
Static functions can only access static variables, and the memory for that will already have been allocated.
Upvotes: 1
Reputation: 4634
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.
Upvotes: 1