1

In Java, for each object, a new copy of instance variables is created which can be accessed using the object reference. But in case of an instance method, only one copy of it(instance method) exists. How is this method accessed by various object references?

Mohit
  • 539
  • 3
  • 9
  • 16

6 Answers6

3

The byte code (or native code if it's JIT'd) for the method is stored in one location. When the method is called, a pointer (under the hood, aka reference at a higher level) to the instance object is passed as the first argument so the method code can operate on that specific instance - have access to its fields, etc. In order to save space without additional performance cost, the calling mechanism in Java is quite a bit more complicated than C++, especially for interface methods.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
2

Methods and fields are completely different. Methods apply to all instances of the object, but fields are per instance.

One way to think of it:
pretend the method is "global" to all instances, but it is "passed" an instance of the object via the "this" reference.

Methods can change the state of a particular instance, but they themselves are stateless.

Brad Cupit
  • 6,530
  • 8
  • 55
  • 60
2

Behind the scenes a reference to the object is passed to the method as part of the call. It may be useful to look at Java's reflection classes, Method.invoke() in particular.

user156605
  • 130
  • 2
1

From a previous answer of mine:

I'm sure the actual implementation is quite different, but let me explain my notion of method dispatch, which models observed behavior accurately.

Pretend that each class has a hash table that maps method signatures (name and parameter types) to an actual chunk of code to implement the method. When the virtual machine attempts to invoke a method on an instance, it gets the object's class, and looks up the requested signature in the class's table. If a method body is found, it is invoked, providing the original object as a reference called this.

Otherwise, the parent class of the class is obtained, and the lookup is repeated there. This proceeds until the method is found, or there are no more parent classes—which results in a NoSuchMethodError.

If a super class and a sub class both have an entry in their tables for the same method signature, the sub class's version is encountered first, and the super class's version is never used—this is an "override".

Community
  • 1
  • 1
erickson
  • 265,237
  • 58
  • 395
  • 493
  • 1
    "Normal" interface dispatch uses an implementation close to that. The keys in the hash table are the interface method being implemented, and the values are the implementing method. Research has come up with some clever [even more complicated] ways to significantly reduce the interface dispatch overhead. Virtual dispatch can be handled more efficiently by an array, assigning a new slot number for each method that doesn't override a base method. This is basically a vtable, only stored in the type descriptor instead of the object. – Sam Harwell Aug 28 '09 at 17:35
1

the implied reference "this" is passed in to each method, which of course you can reference explicitly

user101884
  • 717
  • 3
  • 7
0

I'm assuming you're meaning on a simplistic level, as in how you actually do the call. I'm also assuming you're refering to a method that has the static modifier in its signature, ie:

public static int getNum() 
{
  // code in here 
  return num;
}

If this is what you mean, and this was part of a class called 'SomeClass', then it would be accessed via the method call SomeClass.getNum(). ie, you put the actual class name before the method.

If this is not what you mean, ignore my answer :)

Farrell
  • 508
  • 3
  • 11