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?
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.
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?
All things are lawful, but not all things are profitable.
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
Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]