19

Possible Duplicate:
Java/C# method representation in memory

I was wondering if i have an object in java, how are it's instance methods stored? Does an object have a pointer to it's instance methods?

Example:

public class MyObject {
 private int x;
 private int y;
 [..]
}

If i plan keeping a lot (A big tree of objects for example) of these MyObjects in memory, will it make a significant difference in memory terms if apart from a getter for x and for y i define an instance method calculating the sum?

I'm trying to sort out wether i should add more instance methods or doing the calculations in another class using the getters and setters.

Community
  • 1
  • 1
Samuel
  • 18,286
  • 18
  • 52
  • 88

5 Answers5

18

The instance methods of an object are stored in its class object (only one copy should exist), they are not "copied" with each new instance, instead, under the hood each instance holds a reference to the method implementation residing in the class object.

For a technical explanation, take a look at this chapter from the book "Inside the Java Virtual Machine", in particular the "Object Representation" section; it details how each instance of an object references its methods in the corresponding class. Notice that the implementation details for this are not present in the JVM specification, and they're left open to the implementors - the linked document presents several strategies.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
15

The object needs memory for its attributes (NOTE: for object, the attribute stored is a pointer to the real object, which lies elsewhere in memory). No additional memory is needed for methods (code) as they are stored with the class.

IE:

public class MyClass1 {
  private int myNumber;
}

and

public class MyClass2 {
  private int myNumber;
  public int MyMethod1() {
  }
  public int MyMethod2() {
  }
  public int MyMethod3() {
  }
  ....
  public int MyMethod1000() {
  }
}

use the same memory per instance.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • 1
    By "same memory", do you mean "not just same memory size, but actually the same memory address" ?? – Sarath Chandra Jan 08 '16 at 07:37
  • @SarathChandra same memory size. If it were the same memory address, all of the instance would be reading/writting the same `myNumber` variable. – SJuan76 Jan 08 '16 at 08:05
  • Sorry I (got) confused. What I meant to ask was "all the methods of the class and those of all instances of that class will refer to the same memory location, as there are no 'copies' of methods per instance". Is this right ? – Sarath Chandra Jan 08 '16 at 13:26
  • Right. Methods of class X are part of the `Class` instance , its bytecode being part of the definition of the method http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.3 – SJuan76 Jan 08 '16 at 13:44
6

They are stored inside the class. Details are in the JVM specification.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

No it will not make a significant difference. The method is actually stored in the class definition file MyObject.class. There is only one instance of that method. It gets executed for each instance but is only defined once.

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
1

Each instance is associated to a class.

The class defines the methods for all instances.

Because of this, there is no need to have pointers to methods for each instance.

An instance just store the values of the non static attributes

If you want to know the class of an instance, you just use instance.getClass().

By using getClass() you can access the list of all methods at runtime.

Phil
  • 3,375
  • 3
  • 30
  • 46