Shankara Sharma wrote:One of the restrictions that a static method has is It can only call other static methods and it can't call a non static method from it.
But When i create an object inside a static method, i can call a non static method. Here is the code below:
Can anyone please give an explanation?
[Edit - added code tags - see UseCodeTags for details]
"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Shankara Sharma wrote:
We can even call a static method from non static method directly as static members part of the class.
Does static variables are loaded into memory even before an instance of that class is created?
Does memory is created for static methods and instance methods?
Shankara Sharma wrote:Creation of memory for static and non static methods in the sense, whether the memory is allocated before or after the instance creation?
Shankara Sharma wrote:So for local variables inside methods when will memory be allocated?
Dattatraya Tembare wrote:Yes, it's all about allocation of memory either in Heap or Stack.
And when we call a non-static method it’s part of Stack
Now see there are two separate memory locations which are not linked together so we can’t access static variables/methods from non-static methods.
Jeff Verdegan wrote:
Dattatraya Tembare wrote:Yes, it's all about allocation of memory either in Heap or Stack.
No, the reason for not being able to access non-static variables in a static context has nothing to do with heap vs. stack.
And when we call a non-static method it’s part of Stack
The method's variables go on the stack. The executable code for method body itself, like all methods, is on the heap, as part of the class definition.
Now see there are two separate memory locations which are not linked together so we can’t access static variables/methods from non-static methods.
No, that's not right.
The reason we can't refer to non-static members in a static context has nothing to do with where variables are stored. There is no such concept of one area of memory not being able to see the other.
It's much simpler than that. Non-static members require a "this" reference to a "current" object. There is no "this" and no "current object" in a static context. That's simply a natural consequence of what "static" means in the language and it has nothing whatsoever to do with memory layout.
Dattatraya Tembare wrote:
Please explain me what is this here... this is object of existing class & it's on heap & static context is also on heap. So it's having access. When we call methos it creates copy on stack, we are having reference of object (this) so we are able to access the static data which is on Heap.
I think creating an object is all about allocation of memory, how can you separate this?
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|