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 use
which 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