Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Stack and Heap Variable

 
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i am sought of confused regarding stack and heap variables.

when i say Object o=new Object inside a method, the variable o is placed in the stack and an Object is created in the heap and its address is assigned to o. When we define an Object as an instance variable where is that placed meaning ...

class XYZ
{
Object o;
}

where is Object o now placed, in the stack or the heap ?.

Kind Regards,
Muhammad Mateen
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • Objects are always on the heap.
  • Local variables (declared inside methods) are on the stack.
  • Member variables are part of the data of an object and are on the heap.


  • In your example, the variable 'o' is a member variable, so the variable itself is on the heap. Since objects are always on the heap, the object that 'o' refers to is on the heap.
     
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Say you do something like

    This is what i understand happens: first variable myObj is added to the stack then a new object XYZ is added to the heap. This latter part means that space is allocated for all of the objects member variables, which includes your Object o member. As this member is a reference variable too, it will point to an object (instance of Object) which would be created on the heap as well.
     
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Point to remember

    1- Instance variables and Objects live on Heap.

    2- Local Variables Live on Stack.

    Just i am going through an example to demonstrate clearly

    Conclusion which i think All local will live on stack. and Instance variables and Objects live on Heap

    Many Thanks
     
    author
    Posts: 23909
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Afzal Rehman wrote:
    Conclusion which i think All local will live on stack. and Instance variables and Objects live on Heap



    Of course, partly with Java 6, and then with Java 7, this may be moot -- as it will be possible to put objects on the stack.

    Henry
     
    Afzal Rehman
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks sir,
    but how any reference it would help me to more clear this concepts

     
    Henry Wong
    author
    Posts: 23909
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Afzal Rehman wrote:Thanks sir,
    but how any reference it would help me to more clear this concepts




    Google for "escape analysis". Basically, it is a concept related to performance. If the JVM determines that an object can't escape a method -- created in the method, throw away in the method, nothing else in the method (or class being instantiated) that can cause a reference to leave the method -- then it has the option of instantiating it on the stack. Upon completion, the object is just finalized. There is no need to GC the object, as the memory will simply be reclaimed upon return.

    Henry
     
    Afzal Rehman
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks I will have a look!
     
    reply
      Bookmark Topic Watch Topic
    • New Topic