Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the 'this' variable in Java actually set to the current object?

Consider:

class TestParent{   public int i = 100;   public void printName(){     System.err.println(this); //{TestChild@428} according to the Debugger.     System.err.println(this.i); //this.i is 100.   } }  class TestChild extends TestParent{   public int i = 200; }  public class ThisTest {   public static void main(String[] args) {     new TestChild().printName();   } } 

I know that similar questions have been asked, but I couldn't get a firm understanding of the 'this' variable in Java.

Let me try to explain how I understand the result of the above image.

  1. Since it's a new TestChild() object that's calling the printName() method, the this variable in line 6 is set to a TestChild object - {TestChild@428} according to the Debugger.

  2. However, since Java doesn't have a virtual field - I'm not completely sure what this means, but I conceptually understand it as being the opposite of Java methods, which support Polymorphism - this.i is set to 100 of TestParent at compile time.

  3. So no matter what this is, this.i in a TestParent method will always be the i variable in the TestParent class.

I'm not sure that my understanding is correct so please correct me if I'm wrong.

And also, my main question is,

How is the this variable set to the current object that's calling the method? How is it actually implemented?

like image 391
Kevin Park Avatar asked Sep 16 '16 12:09

Kevin Park


People also ask

Is a reference variable in Java that refers to the current object?

Summary. this Keyword in Java is a reference variable that refers to the current object. this in Java is a reference to the current object, whose method is being called upon. You can use “this” keyword to avoid naming conflicts in the method/constructor of your instance/object.

How do you reference a current object in Java?

The "this" keyword in Java is used as a reference to the current object, within an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables, and methods.

How do you assign an object to a variable in Java?

First, define a class with any name 'SampleClass' and define a constructor method. The constructor will always have the same name as the class name and it does not have a return type. Constructors are used to instantiating variables of the class. Now, using the constructors we can assign values.

What does current object mean in Java?

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this .


1 Answers

In essence, there is no difference between

this.foo() 

and

anyObject.foo() 

as both are "implemented" the same way. Keep in mind that "in the end" "object orientation is only an abstraction, and in "reality" what happens is something like:

foo(callingObject) 

In other words: whenever you use some object reference to call a method ... in the end there isn't a call on some object. Because deep down in assembler and machine code, something like "a call on something" doesn't exist.

What really happens is a call to a function; and the first (implicit/invisible on the source code level) parameter is that object.

BTW: you can actually write that down in Java like:

class Bar {    void foo(Bar this) { ... } 

and later use

new Bar().foo(); 

And for this.fieldA, in the end: you have a reference to some location in memory; and a table that tells you on which "offset" you will find fieldA.

Edit - just for the record. If you are interested in more details about foo(Bar this) - you can turn to this question; giving the details in the Java spec behind it!

like image 189
GhostCat Avatar answered Oct 06 '22 00:10

GhostCat