• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

which memory is used when instance variable of an object is instantiated?

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I know that instance variable is created on stack, and the object which is pointed by this variable is created on heap. But I've confused about instance variable of an object.
To demonstrate, please see the following code:

"per" is created on stack, the object "per" point to is created on Heap
auto-created string "Andy" is created on Heap, and how about the "name"?
It is a member of object Person, on which memory it is created? Heap or Stack?

Thanks,
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
on the heap as usual for the objects!
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, the instance variables are created on the heap, while the local variables are created on the stack.
This is done, as instance variables are part of the object they belong to.

Since per is a local variable, it'll be created on the stack.
But name is an instance variable, which will be created on the heap.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abimaran already answered it correctly while I was composing the reply
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ahem ahem
a little mistake in the question itself


which memory is used when instance variable of an object is instantiated?


this is not correct
instance variables are not instantiated at all
classes are instantiated and those instances are the objects of the class
instance variables are members of that object
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic