Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object life cycle

 
Sindu Ram
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was recently asked a question about an object life cycle in Java, especially with regard to what happens when we do an
A a = new A();
and what goes to the stack and what to the heap.
Any info would be welcome.
Thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables are kept on the stack, while objects are kept on the heap. If this line of code is in a method, then the variable a is on the stack and the A object is on the heap; if it's a line of code at the top level of a class, then a is inside some other object on the heap, and the A is also on the heap.
 
Sindu Ram
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Ernest..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic