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 where variable and objects are stored Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "where variable and objects are stored" Watch "where variable and objects are stored" New topic

where variable and objects are stored

ramchander yshetti
Ranch Hand

Joined: Apr 22, 2006
Posts: 97
in java where variables,methods and objects are stored.
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
objects are stored on the heap

When methods are called they live on the stack.

instance variables are on the heap with its object

local variables(primitive and object references) go on the stack. In the case of an object reference, the object itself of course is on the heap.

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The byte-code for the methods, the static variables and the constants in a class only have to be loaded once. So once a class is loaded onto the heap the JVM can use that stuff all day long. There are some conditions under which this stuff is unloaded, but I wouldn't count on it.

When you instantiate an object the JVM allocates the member variables. These go in the heap as well. Objects are created and garbage collected in different orders which can leave holes in the heap. The GC can move things around to close up the holes and make the remaining contiguous chunk bigger.

When you call a method and as the method executes, the JVM allocates the local variables. These go on the stack. Method calls exit in reverse order of how they were called, so the JVM can de-allocate in reverse order. It doesn't get holes in the stack.

Does that make sense?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
marc weber

Joined: Aug 31, 2004
Posts: 11343

See Where storage lives from Eckel's Thinking in Java.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: where variable and objects are stored
It's not a secret anymore!