• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

'this' keword - wrong use?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Bartender
Posts: 1973
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. I think I see now how it works.
 
Marshal
Posts: 80136
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80136
418
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE MySQL Database Tomcat Server Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how it would look in pictorial form
NetworkDiagram-(1).png
[Thumbnail for NetworkDiagram-(1).png]
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic