This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes instance methods - separate copies? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instance methods - separate copies?" Watch "instance methods - separate copies?" New topic
Author

instance methods - separate copies?

Iris Rawleigh
Greenhorn

Joined: Feb 15, 2009
Posts: 2
Clearly every instance of a class has its own copy of an instance variable. But when it comes to methods, does every instance actually have a separate copy of the code of a non-static method?

Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18916
    
  40

Iris Rawleigh wrote:Clearly every instance of a class has its own copy of an instance variable. But when it comes to methods, does every instance actually have a separate copy of the code of a non-static method?

Thanks


No. The main difference between an instance method and a static method is that an instance method gets a reference to the object that calls it (the "this" instance). The actual location of the methods are not stored with the instances.

Henry
Iris Rawleigh
Greenhorn

Joined: Feb 15, 2009
Posts: 2
Henry Wong wrote:
No. The main difference between an instance method and a static method is that an instance method gets a reference to the object that calls it (the "this" instance). The actual location of the methods are not stored with the instances.

Henry


Ah, ok that makes more sense. Are these references stored on the stack?

Iris
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18916
    
  40

Iris Rawleigh wrote:
Ah, ok that makes more sense. Are these references stored on the stack?

Iris


EDIT >>>> Sorry, mis-read you question.... Yes, the "this" reference is one of the parameters to the method, so yes, it is pushed onto the stack during the call.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: instance methods - separate copies?