Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

a question about "this" keyword

 
Ranch Hand
Posts: 424
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
 
Marshal
Posts: 15894
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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
Marshal
Posts: 15894
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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.
 
Ranch Hand
Posts: 109
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: 424
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!!!
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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.

 
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.
 
Ranch Hand
Posts: 99
1
Eclipse IDE MySQL Database Chrome
  • 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.
 
Marshal
Posts: 70351
283
  • 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
 
Master Rancher
Posts: 4669
49
  • 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.
 
The only cure for that is hours of television radiation. And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic