4

If not, how exactly at run time, do the instance methods use the instance variables of the same object? For example I have a class A, having instance variable aVariable and non static method aMethod(). Now I have an object of class A, let's name it aObject. aMethod makes use of aVariable. When I invoke this method, how does it know, in memory, which aVariable to use, and where it is stored? Do we pass all the object information to the method? Please help me with this.

Aditi Garg
  • 320
  • 2
  • 9

5 Answers5

8

Refer to the JVM Spec, Sec 3.7:

int add12and13() {
    return addTwo(12, 13);
  }

This compiles to:

Method int add12and13()
0   aload_0             // Push local variable 0 (this)
1   bipush 12           // Push int constant 12
3   bipush 13           // Push int constant 13
5   invokevirtual #4    // Method Example.addtwo(II)I
8   ireturn             // Return int on top of operand stack;
                        // it is the int result of addTwo()

The invocation is set up by first pushing a reference to the current instance, this, on to the operand stack. The method invocation's arguments, int values 12 and 13, are then pushed.

So, the this reference (or any other value of the receiver parameter) is just pushed onto the stack as another argument. As such, separate copies of the method are not required per instance.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
3

No, we do not have separate copies for instance methods. The way this is done is by passing an implied reference to this:

For a method

public void doSomething(Object param);

, calling

this.doSomething(myParameter);

is resolved to

doSomething(this, myParameter);
daniu
  • 14,137
  • 4
  • 32
  • 53
1

It uses the instance method of that particular instance of the class:

public class Person{
  private String name;
  public Person(String name){
    this.name = name;
  }

  public void printName(){
    System.out.println(name);
    System.out.println(this.name);
  }
}

here printName uses the name variable. Let's say we have two instances of Person

Person a = new Person("Andrew");
Person b = new Person("Bert");

When you call an instance member on an(y) of these instances, that instance is not aware of any other instances but itself, so, if you call a.printName() it will always print Andrew, since that's the name stored in the a variable, and if you call b.printName() it will always print Bert.

As you can see, adding this. doesn't change anything in the printName method, it's only needed if there is ambiguity, because there is a local variable with the same name.

But it will always take the value from the current instance.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
0

When you create an object "aObject" and you call the method with aObject.aMethod(), you use "aObject" which is where you are specifying which instance you want to call that method on !

And obviously when you create multiple instances you create multiple memory allocations, which are located in different places.

The instance variables are just a link or a pointer to the location of the memory where all the related data for that instance is stored.

Sujal Mandal
  • 975
  • 1
  • 14
  • 29
0

First you should understand how memory works when you create a new instance of an object. To sum it up, when you make a new instance of an object, it makes space in memory to allocate all its variables in a whole stack for the instance. It's very easy to find exactly how it works on the internet, but that works for a basic explanation.

It's important to know, though, that there are other variables that don't work that way. For example, the static variables exist, which behave in a way that if you change the value of this variable, it will change in all the instances of the object.

But in general, you have to imagine a stack of variables of the object that will make an instance, and a stack of those instances that will make the final program

Shinra tensei
  • 1,283
  • 9
  • 21