• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding this java keyword better  RSS feed

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the code :



I m having issues understanding System.out.println(getInfo());
First of all when you invoke a method and you don't use the object.methodCall() it is equivalent to using this.methodCall() right ? In that case it makes sense to me for this to refer to the object/class from where the method is called (in our case Person) .But it's not like that as the result will confirm the method getInfo overridden in Student class is used , meaning that this references to the Student class/object when the statement new Student().printPerson(); is executed . Can anyone explain why is that as it does make sense for me .

Thanks
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:First of all when you invoke a method and you don't use the object.methodCall() it is equivalent to using this.methodCall() right ?
Yes

Tiberius Marius wrote:In that case it makes sense to me for this to refer to the object/class from where the method is called
object yes, class no. They are not the same thing

Tiberius Marius wrote: (in our case Person)
On line 3, you create a Person object, so the getInfo method defined in the Person class will be called.
On line 4, you create a Student object, so the getInfo method defined in the Student class will be called.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that your example won't compile.
Person and Student are inner classes of Test class. You try to instantiate them in static method which is illegal.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Note that your example won't compile.
Person and Student are inner classes of Test class. You try to instantiate them in static method which is illegal.


I m sorry , i type it wrong . I corrected the use of {} now .

Tiberius Marius wrote:In that case it makes sense to me for this to refer to the object/class from where the method is called
object yes, class no. They are not the same thing

I used class too because i imagined this code would compile , it does not


Tiberius Marius wrote: (in our case Person)
On line 3, you create a Person object, so the getInfo method defined in the Person class will be called.
On line 4, you create a Student object, so the getInfo method defined in the Student class will be called.


Here is the issue with my understanding , because of line 4 it will call this.System.out.println(this.getInfo()); but it is not very logical for me as from what i read about this it refers to the object itself.Once the method println is used in the Person class this still does not refere to Person instance . What i m looking for is a better explanation about what "this" does (and i have read it already in 2 books but this example still baffles me )
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great questions! Even better that they can all be answered by making just a small change to your code. To help me explain I decided to move your inner classes of Person and Student by removing them as inner classes and making them into a two class hierarchy.



Now that the code is legal it makes it a bit easier to help!

To start, let's take a look at where it starts at line 5. We start by instantiating a new Person object and simultaneously invoke the printPerson() method on the new Person object. When the printPerson() method is invoked, with the Person object in mind, a new line of code is run. This line of code is System.out.println(getInfo()). And yes, you are right that normally the invocation would like something like Object.methodCall() and you are also right that when the object reference does not explicitly appear that this.methodCall() is the implicit standard. The 'this' keyword is used when referring to the object being currently used. So, which object is it? We used the Person object to call the printPerson() method so therefore the object currently being used must be the Person object we created in the main method. So, instead of looking at the getInfo() call as this.getInfo() we can think of it as objectOfTypePerson.getInfo(). This invocation, of course, is incorrect. There never was an assigned reference variable for this Person object so I have no name to refer to it by. If we instantiated the Person object as Person fred = new Person() I could have said fred.getInfo(). The other thing is which method does it use? Does the JVM use the overridden getInfo() method in Student, or the getInfo() in Person? Since we are using an object of type Person, it makes sense to use the getInfo() method in the class Person, rather than using getInfo() in class Student.

The same is true for when instantiating the new Student object and invoking the printPerson() method. We use the Student object for the whole process. We call printPerson() on it, we access the printPerson() method, the printPerson() method implicitly uses the Student object to invoke the getInfo() method, the getInfo() method takes us to the getInfo() method in class Student since we are invoking the method with an object of type Student and finally getInfo() returns a String "Student" which is then printed by the printPerson() method.

I really hope this helped you and good luck!
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:I m sorry , i type it wrong . I corrected the use of {} now .

Please do not edit your post once it is answered. It made my reply look like nonsense.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explication . It made more sense but i will also look for the definition of this in another book because what i read already about "this" is confusing . I understand why the methods are used , i mean the declared type vs the actual type and why the method in student is used instead of the one in person for the second statement in the main method .
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try using the Java Tutorial on the 'this' keyword. That may help you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!