• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instance reference variables

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, instance variables are stored inside objects, so where are instance reference variables stored? I know that local instance variables are stored on the stack.

class A{
A ref=new A();
void method(){
}

ref variable can be used to refer to the A instance, is 'ref' stored inside the same A instance that i want to refer? Or is it stored in the heap separately?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only local variables are put into Stack, everything else goes to Heap.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My interpretation is that the variable ref is stored on a stack, but the object ref is refering to is stored on the heap.
 
Saloon Keeper
Posts: 15561
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. 'ref' is stored in the same place where the A instance is stored. The object referred to by ref may be either on the stack or the heap, depending on "escape analysis" performed by the compiler.

Local variables are always stored on the stack. Objects may be stored on either the stack or the heap. Instance variables are always stored together with their enclosing object.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic