File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes do objects have seprate copies of the instance methods as they have separate copies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "do objects have seprate copies of the instance methods as they have separate copies " Watch "do objects have seprate copies of the instance methods as they have separate copies " New topic
Author

do objects have seprate copies of the instance methods as they have separate copies

Jitin Guglani
Greenhorn

Joined: Jan 01, 2009
Posts: 17
Hello all
may be my question is very stupid or might be very simple but I am really confused because I searched every where but did not find answer.
My question is that "do objects have seprate copies of the instance methods as they have separate copies instance variables"
also to support your answer kindly give the reference of the book/site or authenticate evidence.
thanks in advance
Jitin Guglani
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

No, it's not stupid, but it is a misunderstanding of how programs work. Methods are just instructions to the computer, so you don't need different "instances" of the instructions for different objects. You just tell your program that you want to run the given set of instructions using a particular set data that you give it as parameters. Now things like parameters and local variables do have separate copies for each method call. They are stored on the stack, which is just an area of memory your program uses for temporary storage. When you call a method, all the parameters are put on the stack, and any local variables are also put there. When you exit the method, the stack is reset so these values are gone.

I highly recommend an operating systems course, or something where you get to see how computers and computer programs work at a low level. It can be really fascinating if you are the kind of person who is always curious about how things work, which it seems like you are. I also think it's important to understand how things work at a low level, even if we as engineers are often abstracted from that.
nimish kumar
Ranch Hand

Joined: Dec 04, 2009
Posts: 56
Greg Charles wrote:No, it's not stupid, but it is a misunderstanding of how programs work. Methods are just instructions to the computer, so you don't need different "instances" of the instructions for different objects. You just tell your program that you want to run the given set of instructions using a particular set data that you give it as parameters. Now things like parameters and local variables do have separate copies for each method call. They are stored on the stack, which is just an area of memory your program uses for temporary storage. When you call a method, all the parameters are put on the stack, and any local variables are also put there. When you exit the method, the stack is reset so these values are gone.

I highly recommend an operating systems course, or something where you get to see how computers and computer programs work at a low level. It can be really fascinating if you are the kind of person who is always curious about how things work, which it seems like you are. I also think it's important to understand how things work at a low level, even if we as engineers are often abstracted from that.


Hi Greg,
So you are saying there will be only one copy of method irrespective of number of instances of that class created?
If thats true, then does this hold good for all methods or only static methods. Static methods will have only one copy for any number of instance of class created.
If what you say is only copy for any number of instances of class, then whats the difference between static and non static methods
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

nimish kumar wrote:
If what you say is only copy for any number of instances of class, then whats the difference between static and non static methods


Static methods are called with a context that doesn't include a particular instance. And it can be called either via an instance or not. Instance methods are called with a context that includes an instance. And the method can access the instance with the "this" variable.


BTW, out of general interest, why do you think that instances need to have their own copies of methods? Isn't that just a lot of wasted space?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Praneeth Kunala
Greenhorn

Joined: Oct 27, 2007
Posts: 1
Hi Nimish

To invoke a static method it's not mandatory to have an instance of the object , but for a non static method you do need one.

There may be necessity to use a method in a class without an instance of the class like the classic Class.forName() .
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

nimish kumar wrote:
So you are saying there will be only one copy of method irrespective of number of instances of that class created?


Yes, that's right. Again, a method is just a set of instructions. There's no reason to have more than one copy of it. There's good reason to have different stacks, so if two threads are calling the same method on different instances (or even the same instance), they don't interfere with each other. Imagine two cooks baking cakes. They can be reading the same recipe from the same book, but they need their own ingredients, bowls, ovens, etc.

nimish kumar wrote:
If thats true, then does this hold good for all methods or only static methods. Static methods will have only one copy for any number of instance of class created.
If what you say is only copy for any number of instances of class, then whats the difference between static and non static methods


On a concrete level, the only difference between a static method and an instance method is that the compiler secretly adds the instance as a parameter to instance method calls. You've probably seen that you can refer to the instance under the name "this", even though it's not listed in the parameters. That's why. It's just a simple trick, but it's one of the fundamental things that enables object-oriented languages.
Jitin Guglani
Greenhorn

Joined: Jan 01, 2009
Posts: 17
Hi all and really thanks a lot to all (specially Greg) for providing the details and clearing my doubts and please Greg can you provide me link to any sun decumentation where this information is specified ( it is such a important thing that they should atleast say something about it)
Also kindly think on this from another prespective that if the objects have their own copy of methods they can be fast as no more jump to the common method area will be required. (ofcourse memory will be constrained in that).
But I am very much intrested in looking this information from sun documentation which I really searched very hard but it looks like they are silent in this issue
or it might be that i am missing.
kindly help me to resolve this issue.
thnaks in advance
jitin


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

Jitin Guglani wrote:Hi all and really thanks a lot to all (specially Greg) for providing the details and clearing my doubts and please Greg can you provide me link to any sun decumentation where this information is specified ( it is such a important thing that they should atleast say something about it)

The way this works is not specific to Java, it works like this in any object oriented language that I know of.

The code in a method is just a fixed list of instructions, which is called to work on the data of a specific object (which the this reference refers to when the method is running). It would be strange to copy that fixed list of instructions for each instance of a class. You'd end up with a lot of unnecessary copies of the exact same list of instructions, which only wastes memory.

Jitin Guglani wrote:Also kindly think on this from another prespective that if the objects have their own copy of methods they can be fast as no more jump to the common method area will be required. (ofcourse memory will be constrained in that).

I don't think that would help a lot, in fact, it would most likely hurt performance. Note that the CPU in your computer has a small, but super fast cache built-in. If the CPU can load the code for a method in its cache, it can quickly execute it any number of times. If each method would have its own copy of the instructions, then each time you'd call the same method on a different object, the CPU would first have to load the instructions from main memory into the cache, which slows down things considerably.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jitin Guglani
Greenhorn

Joined: Jan 01, 2009
Posts: 17
hi Jasper
thanks for your opinion and comments but have you found any sun specifications regarding this . Please I am despirately looking for sun comments on this issue.
with regards
Note : I am already convinced but wanted to know that what Sun Specifications says about it, so kindly provide me the link for it .
Thanks in advance.
jitin
nimish kumar
Ranch Hand

Joined: Dec 04, 2009
Posts: 56
Henry Wong wrote:
nimish kumar wrote:
If what you say is only copy for any number of instances of class, then whats the difference between static and non static methods


Static methods are called with a context that doesn't include a particular instance. And it can be called either via an instance or not. Instance methods are called with a context that includes an instance. And the method can access the instance with the "this" variable.


BTW, out of general interest, why do you think that instances need to have their own copies of methods? Isn't that just a lot of wasted space?

Henry


Henry,
Ya that's true, it will take more space.
nimish kumar
Ranch Hand

Joined: Dec 04, 2009
Posts: 56
Greg Charles wrote:
nimish kumar wrote:
So you are saying there will be only one copy of method irrespective of number of instances of that class created?


Yes, that's right. Again, a method is just a set of instructions. There's no reason to have more than one copy of it. There's good reason to have different stacks, so if two threads are calling the same method on different instances (or even the same instance), they don't interfere with each other. Imagine two cooks baking cakes. They can be reading the same recipe from the same book, but they need their own ingredients, bowls, ovens, etc.

nimish kumar wrote:
If thats true, then does this hold good for all methods or only static methods. Static methods will have only one copy for any number of instance of class created.
If what you say is only copy for any number of instances of class, then whats the difference between static and non static methods


On a concrete level, the only difference between a static method and an instance method is that the compiler secretly adds the instance as a parameter to instance method calls. You've probably seen that you can refer to the instance under the name "this", even though it's not listed in the parameters. That's why. It's just a simple trick, but it's one of the fundamental things that enables object-oriented languages.


Thanks Greg, Now its clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: do objects have seprate copies of the instance methods as they have separate copies