• Post Reply Bookmark Topic Watch Topic
  • New Topic

a question about "this" keyword  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

wrote a simple bridge length program that only lets you create a bridge no less than 20m up to 150m using
accessors and mutators...

here is the code:



i removed "this" from:
this.bridgeLength = bridgeLength;

now it always gives me 0 as the value...im sure its pretty simple (and i prob should know this by now)
what value is the .this giving the variable

from what i read...this will use the instance variable instead of the local one right?
but i thought nothing was assigned to the local variable,its just declared...is that why it gets 0

think im confused:
local variables are assigned null,0 if no assignment right? is it instance variables that wont give a default value?

thanks
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:
think im confused:
local variables are assigned null,0 if no assignment right? is it instance variables that wont give a default value?

You are confused; that's backwards.  Instance variables are assigned default values according to their type. Local variables do not get assigned default values and must be explicitly initialized.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you get 0 is because that's the default value that the bridgeLength field will be assigned.  If you remove the "this." on the left side of the assignment statement on line 7, then you will effectively be assigning the value of the parameter to itself. This does not affect the bridgeLength field so your getBridgeLength() method returns 0.

Your setBridgeLength() also assigns whatever value you pass to it, regardless of whether or not it is a restricted value.

Also, your if statement allows 19 and 151. This contradicts your requirement that a bridge should be no less than 20m and up to 150m.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The reason you get 0 is because that's the default value that the bridgeLength field will be assigned.  If you remove the "this." on the left side of the assignment statement on line 7, then you will effectively be assigning the value of the parameter to itself. This does not affect the bridgeLength field so your getBridgeLength() method returns 0.

Your setBridgeLength() also assigns whatever value you pass to it, regardless of whether or not it is a restricted value.

Also, your if statement allows 19 and 151. This contradicts your requirement that a bridge should be no less than 20m and up to 150m.


Did he manage to edit his code? Because I see it's working fine.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"Instance variables are assigned default values according to their type. Local variables do not get assigned default values and must be explicitly initialized."
ok so you could go int x; in a method (local variable)
but you couldnt go System.out.print(x); this will error...but an instance variable you could...0 would be printed...right?

"The reason you get 0 is because that's the default value that the bridgeLength field will be assigned.  If you remove the "this." on the left side of the assignment statement on line 7, then you will effectively be assigning the value of the parameter to itself. This does not affect the bridgeLength field so your getBridgeLength() method returns 0." -- thanks,cleared that up...

Also, your if statement allows 19 and 151. This contradicts your requirement that a bridge should be no less than 20m and up to 150m. -- yes was just testing the code before i put it up here...i understand this

thanks all!!!
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:
"Instance variables are assigned default values according to their type. Local variables do not get assigned default values and must be explicitly initialized."
ok so you could go int x; in a method (local variable)
but you couldnt go System.out.print(x); this will error...but an instance variable you could...0 would be printed...right?

Right.

 
Anton Golovin
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:hi guys,

wrote a simple bridge length program that only lets you create a bridge no less than 20m up to 150m using
accessors and mutators...

here is the code:



i removed "this" from:
this.bridgeLength = bridgeLength;

now it always gives me 0 as the value...im sure its pretty simple (and i prob should know this by now)
what value is the .this giving the variable

from what i read...this will use the instance variable instead of the local one right?
but i thought nothing was assigned to the local variable,its just declared...is that why it gets 0

think im confused:
local variables are assigned null,0 if no assignment right? is it instance variables that wont give a default value?

thanks


Hi, Jon,

When you don't use in this particular instance, the variable you're using then is the same as the variable of the parameter you're passing in, and it exists only for the duration of the method run, hence you never set the instance variable in your class, which is initialized by default to 0 and is never altered by you later one.

With best regards,

Anton.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lol, this is a classic mistake. Easy to do and easy to avoid in future.

You have to be VERY careful when giving a method param the same name as the instance variable it is going to set. If you leave 'this.' out you will be updating the parameter variable, not the instance variable i.e. updating a var scoped to the setter method.Personally I would avoid calling a method param the same name as the instance variable to be updated. Just means one less possible source of error.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot of people think the exposure of a well‑thought‑out variable name in a constructor or setXXX method outweighs the awkwardness of remembering to write this.xxx
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
On the assumption the internal variable has a sensible name then, in attempting to avoid using the sensible name as a parameter name, you end up with either an abbreviation or some bolted on prefix, neither of which are as good as the sensible name you already have.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!