• Post Reply Bookmark Topic Watch Topic
  • New Topic

Who is the actual occupupant???  RSS feed

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

When we initialize an object, may be by initializing its member variables, we actually create an object reference variable which points to the actual object in the Heap(where object is loaded). My question is what actually occupies memory when we say every object occupies space, ofcourse the member variables initialized do but will an instance method aslo occupy space? if yes then how?

thanx
amit
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything will occupy some space in memory. Variables and methods have to be stored so that you can use them. I'm not sure how Java organizes these structures, but they are all there in memory.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This link may give the information you are looking for

http://www.artima.com/designtechniques/initialization.html
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit --

The answer to your question is no, methods aren't stored in the heap space for each instance of a class. They're stored in one place, and that single copy is used for all the instances of that class.
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects just remember which class they are an instance of, the method's code is only hold in memory once per class. Local variables are only alive during the execution of a method and live on the Stack.

Does that answer your question?
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Objects just remember which class they are an instance of, the method's code is only hold in memory once per class. Local variables are only alive during the execution of a method and live on the Stack.

Does that answer your question?


Be careful here. The object which a local variable points to (if it's an object reference) is on the heap, not on the stack.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!