File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Memory Allocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Memory Allocation" Watch "Memory Allocation" New topic

Memory Allocation

Sam Drake
Ranch Hand

Joined: Jun 05, 2003
Posts: 33
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.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
[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 agree. Here's the link:
subject: Memory Allocation
It's not a secret anymore!