• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

This confuses me (pun intended)

 
Greenhorn
Posts: 15
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't wrap my head around this one, someone please explain, almost 2 identical classes, only difference is, the call to this is in different methods

Class 1 gives output of:
Hey Willem
Hey Dude



Class 2 gives output of:
Hey Dude
Hey Dude

 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The methods introduce another greet variable. So at any point inside the method there are actually two references to two different objects both with the same variable name greet.

 
Ranch Hand
Posts: 40
2
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how, I see this.

The greet variable in method niceMethod(String greet) in class WTF is just a local variable to this method and without using the "this" keyword it just reassign the value to itself instead of assigning the instance variable greet.

The niceMethod(String greet) in class WTF2 assign the value to the instance variable greet because of this.greet while the nicemethod2(String greet) is doing the same thing as nicemethod(String greet) in class WTF.

Now, when we print value of wtf.greet:

1. In class WTF, wtf.greet is set by nicemethod2(String greet) and before its only using its first initialized value "String greet = "Hey Willem";"
2. In class WTF2, wtf.greet is set by nicemethod(String greet) and nicemethod2(String greet) will not change the value of wtf.greet so whenever you print wtf.greet it will show the value set by nicemethod.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, your comment in the WTF class makes no sense at all! You are invoking niceMethod and niceMethod2 both with "Hey Dude". So it's completely impossible that "Hey Willem" is assigned. The "Hey Willem" is assigned to instance variable greet when you create a new instance of WTF, so in this line:

So you have to wonder why "Hey Dude" is not assigned to the instance variable greet when invoking niceMethod. And that's easy: in niceMethod you have a method parameter with the same name as an instance variable, that's called variable shadowing. It's allowed in Java (no compiler error), because you can make a distinction between both variables:
  • use variableName to use the method parameter (or local variable)
  • use this.variableName to use the instance variable


  • So in the niceMethod method of the WTF class you usewhich simply assigns the method parameter to the method parameter (and not to the instance variable).

    Exactly the same applies to the WTF2 class, but in this class it's the niceMethod2 method which simply assigns the method parameter to the method parameter (and not to the instance variable).

    Hope it helps!
    Kind regards,
    Roel
     
    Willem De Bruyn
    Greenhorn
    Posts: 15
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:
    So in the niceMethod method of the WTF class you usewhich simply assigns the method parameter to the method parameter (and not to the instance variable).l



    Mmmm if I just had to read the code for the 1st time that would'e been my exact thought as well, BUT then how come when niceMethod in WTF class gets invoked and passed the String "Hey Dude" it still prints the value of the instance variable "Hey Willem"...
     
    Willem De Bruyn
    Greenhorn
    Posts: 15
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Kaleem Anwar wrote:This is how, I see this.

    The greet variable in method niceMethod(String greet) in class WTF is just a local variable to this method and without using the "this" keyword it just reassign the value to itself instead of assigning the instance variable greet.

    The niceMethod(String greet) in class WTF2 assign the value to the instance variable greet because of this.greet while the nicemethod2(String greet) is doing the same thing as nicemethod(String greet) in class WTF.

    Now, when we print value of wtf.greet:

    1. In class WTF, wtf.greet is set by nicemethod2(String greet) and before its only using its first initialized value "String greet = "Hey Willem";"
    2. In class WTF2, wtf.greet is set by nicemethod(String greet) and nicemethod2(String greet) will not change the value of wtf.greet so whenever you print wtf.greet it will show the value set by nicemethod.



    After re reading this a couple of times it makes better sense now, thanks
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Willem De Bruyn wrote:BUT then how come when niceMethod in WTF class gets invoked and passed the String "Hey Dude" it still prints the value of the instance variable "Hey Willem"...


    Because that's the actual value of the instance variable greet (as niceMethod doesn't change the instance variable). Clearly illustrated in this code snippet:Output:
    Hey Willem
    in niceMethod before: instance=Hey Willem method=Hey Dude
    in niceMethod after: instance=Hey Willem method=Hey Dude
    Hey Willem
    in niceMethod2 before: instance=Hey Willem method=Hey Dude
    in niceMethod2 after: instance=Hey Dude method=Hey Dude
    Hey Dude


    Hope it helps!
    Kind regards,
    Roel
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic