Forums Register Login

Memory Allocation

+Pie Number of slices to send: Send
I have a doubt regarding how memory is allocated for instance objects in JAVA. Can you please answer the following questions.
[1] How is memory allocated for methods in a class (both static and non-static)?
[2] Suppose I have an inner class within an enclosing classes, is memory allocated for the inner class too or unless its instantiated?
[3] How come the inner class can access instance variables inside the enclosing class and NOT vice-versa?
Kindly reply in detail.
Thanx a lot in advance.
regards,
Sam.
+Pie Number of slices to send: Send
[1]. Primitive values are always located on the stack. Reference objects are created and live on the heap, while each's reference variable lives on the stack like the rest of the primitives (assuming it is being used somewhere). (Sorry for the incorrect term-usage there, if it applies.)
Static or non-static? Huh? A method of execution is a method of execution and the rules will always apply concerning where things live and die (ie heap or stack). So, to correct you wording ... you should have said "how is memory for variables allocated and where for some methods..."
[2]. Inner classes are just regular classes that are um .. inner'ed. So, like all the other cool classes, they're just blueprings for objects which you will eventually create (on the heap) and only then will you be using memory due to your inner class.
Technically, however, there is an actual "class object" floating around somewhere for your inner class so if you had static members in there (variables) it would incur some memory usage.
[3]. Think of an enclosing class and its inner class as a uni-directional association. The child needs to know who its daddy is, but the daddy don't care (paraphrase).
If you really needed to get at an inner class instance's data, you could store it in a regular ol' instance variable in the enclosing class and invoke methods on it just as you normally would.
Hm, yeah I guess that's about it. I should go to bed -- certification test in the morning. Hope my rambling helps you though.
I am mighty! And this is a mighty small ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1396 times.
Similar Threads
Quick Question
can a class be declared static?
Inner
Static inner class
Inner classes; when should they be static vs. instance?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:37:01.