Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

'this' keword - wrong use?  RSS feed

 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there my Friends! I'm back with more questions.
OK. I came across such a bizarre (still doing my theory practice tests) piece of code :



The whole program compiles and works fine, what puzzles me is this line:



I don't understand how it works. Anyone cares to explain?

whole program looks like this:

 
Mike London
Ranch Hand
Posts: 1476
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamila Bertran wrote:Hey there my Friends! I'm back with more questions.
. . .
\

I think it might be helpful for you if you ran this code in a debugger and looked at the "this" reference and other values.

Looking at Java code by itself can be less than helpful - for anyone.

With a needlessly confusing piece of Java code like the one you posted, it's also useful to use the debugger to "step through" the code to really see/understand what it's doing.

See the debugger screenshot below.

HOPE THIS HELPS.

- mike
network.png
[Thumbnail for network.png]
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamila Bertran wrote:The whole program compiles and works fine, what puzzles me is this line:

The Java™ Tutorials wrote: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.
you can read about it here Using this keyword
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I think I see now how it works.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike London wrote:. . .  needlessly confusing piece of Java code . . .
That is a good assessment of the question. I do not know whether such confusing code comes up in a cert exam, but that constructor appears to set up the p field to point to the same Network as n, unless n points to null in which case p points to the current object. I don't think you will get an infinite regression there, but that can happen with objects which have themselves as a field.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: I do not know whether such confusing code comes up in a cert exam, ...


I've always had the impression that it does, sadly.
There's got to be a reason a lot these dummy questions take the same sort of format.
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: I do not know whether such confusing code comes up in a cert exam...


Yes it does. Unfortunately.

What I still don't get though how does this call:

devolves value of id as 1. Does p.p.id is like super call?
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Network object has an attribute called 'p' which is a Network.
That Network object has an attribute called 'p' (assuming it isn't null).

So n3.p is the Network attribute 'p' of the Network object 'n3'.
n3.p.p is the 'p' attribute of that one.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamila Bertran wrote:. . . Does p.p.id is like super call?
Not quite like super.xxx
I suggest you draw squares representing the different objects created in that code, with arrows to each. You will find that n3 has a p field, and when you follow that, it has a p field, and that has an id field.

If you wrote code like that anywhere but in a cert exam, it would be marked as very poor design, and you would fail your assessment, lose your job, or whatever.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I still don't get though how does this call: 

It is a little confusing, huh?

First there is a variable n3 that contains an object reference.  n3 has a member (in this case a field) called p which is also contains an object reference.  This first p has a member that is also called p and it contains an object reference.  The second p has a member called id with holds an integer value.  That is what's printed.
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you wrote code like that anywhere but in a cert exam, it would be marked as very poor design, and you would fail your assessment, lose your job, or whatever.


I can see why. It's a terribly written code. Makes my eyes go all funny.


But I think I get it now. Just need to be really careful reading the code.

Thanks again!
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how it would look in pictorial form
NetworkDiagram-(1).png
[Thumbnail for NetworkDiagram-(1).png]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!