• Post Reply Bookmark Topic Watch Topic
  • New Topic

Memory allocation for objects & variables?  RSS feed

 
kavitha vasu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have attended one Interview . They asked whether the memory will be allocated for an object or variables . Please any one give clear answer for this

EDIT by mw: Changed subject line to be descriptive (was "java classes").
[ February 22, 2007: Message edited by: marc weber ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"moon",

Please read our policy on Screen Names. Follow the link on that page, and update your profile. This is not optional.

Second, I'd advice you read some of the topics here, specifically ShowSomeEffort and UseAMeaningfulSubjectLine. You have two threads with the same subject of "Java Classes", which is rather vague. Something like "Size of classes in Java" and "Memory allocation for objects and variables" would have been much better.

To answer your question, both.

reference variables and primitives are on the stack, and get allocated in the stack frame (i think i have my terminology correct). They get automatically deallocated when the frame goes out of scope.

objects are allocated on the heap, as they are created. They get garbage collected when there are no more active references to them, and the JVM feels like doing a GC.
[ February 22, 2007: Message edited by: Fred Rosenberger ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!