sjain
sjain

Reputation: 1725

Does methods have different copies for eact object?

Hi
I have a class Sample in java. It has two instance variables and one method to add the instance variable and print the sum.
I created two Objects of Sample class. Its clear to me that both objects will have their own data members. I have to know that will both the object have one common copy of the method to which both uses or they have different copy in jvm.

Upvotes: 1

Views: 66

Answers (2)

Edwin Buck
Edwin Buck

Reputation: 70929

Generally speaking, the JVM keeps the instructions separate from the data in memory layout, and the "code" of the method is referred to by the object. This means that two instances calling the same method will generally both reference the same "code" block in the memory layout.

Upvotes: 2

WW.
WW.

Reputation: 24311

The class is loaded once by the JVM. So there is one common copy of the method in memory.

Upvotes: 1

Related Questions