Results 1 to 11 of 11
- 01-15-2013, 08:14 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Overloaded Variables - Method doesn't seem to work?
Hi all,
I have this bit of code from a textbook question about overloaded variables that is really confusing me:
So basically I understand that the variables in methodOne are local variables and the ones declared in the top are instance variables.class randomClass
{
private int a = 0;
private int b = 10;
public void methodOne()
{
int b = 11;
a = 10;
b = 20;
}
public void methodTwo()
{
System.out.println(a + "," + b );
}
}
What I don't understand is that when I make an instance of the class its set values are "a = 0, b = 10".
But when I send it the messages:
object.methodOne();
followed by
object.methodTwo();
The answer will display
a = 10, b = 10.
Basically methodOne() sets the objects values to "a = 10, b = 10" (as reported by the string that is returned from the second method).
I do not understand as this means methodOne is changing the value of a, but not the value of b.
Surely it should read: a = 10, b = 20 (or b = 11)
I have been trying to figure this out for a while and gave up and decided to post here for further guideance.
Why is methodOne() only changing the values of 1 of the instance variables, specifically "a".
I understand if in methodOne() we change the local variables to "this.a = 10" and "this.b = 20" it will change both as they will reference the instance variables and change them, but this isn't the case.Last edited by bbfunk; 01-15-2013 at 08:22 PM.
- 01-15-2013, 08:20 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Overloaded Variables - Method doesn't seem to work?
Your methodOne() method has its own local variable b and it 'shadows' the instance variable b; if you change b, the local variable b will be changed and it stops existing when your method is finished; so setting it to 20 only sets the local variable b to 20 and it is gone when the method has stopped. The instance variable b is still equal to 10.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-15-2013, 09:46 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Overloaded Variables - Method doesn't seem to work?
- 01-15-2013, 11:34 PM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: Overloaded Variables - Method doesn't seem to work?
The trick is that methodOne() has its own variable with the same name as instance variable b. This is done by declaring new local variable, inside method's body, which happen to have the same name as instance one.
This means that instance variable b will be "overwritten" internally (inside method's body) with this local variable b. methodOne() will work with its local variable and will ignore instance variable with the same name.Java Code:public void methodOne() { int b = 11; // brand new, local variable with the same name as instance variable
At the same time, methodOne() does not have local variable a and only available variable with that name is instance variable a. So instance variable is used. Any clearer?
For better understanding you can try to delete or just comment out this line in method's body
and you will see that method will change value of instance variable b to 20.Java Code:int b = 11;
- 01-15-2013, 11:44 PM #5
Re: Overloaded Variables - Method doesn't seem to work?
Where is the variable a defined?then a should = 0.If you don't understand my response, don't ignore it, ask a question.
- 01-16-2013, 02:12 AM #6
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Overloaded Variables - Method doesn't seem to work?
Last edited by bbfunk; 01-16-2013 at 02:18 AM.
- 01-16-2013, 02:15 AM #7
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Overloaded Variables - Method doesn't seem to work?
In the class, so each new object of the class should initialize a = 10.
This is true when I create a new object of this class. Its properties show "a = 0" "b = 10"
But as soon as I run methodOne(), "a" changes to "10". How is this possible if there is no variable defined for it?
As milovan said "methodOne() does not have local variable a and only available variable with that name is instance variable a."
If this is the case, then why is "a" changing its value from 0 to 10 when I run methodOne()?
This is v.v.confusing.Last edited by bbfunk; 01-16-2013 at 02:17 AM.
- 01-16-2013, 02:24 AM #8
Re: Overloaded Variables - Method doesn't seem to work?
Because of this assignment statement in methodOne:why is "a" changing its value from 0 to 10 when I run methodOne()?It sets a to 10.Java Code:a = 10;
No, the following code sets a to 0 when a new instance of the class is created.so each new object of the class should initialize a = 10.
Java Code:private int a = 0;
Last edited by Norm; 01-16-2013 at 02:26 AM.
If you don't understand my response, don't ignore it, ask a question.
- 01-16-2013, 02:45 AM #9
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Overloaded Variables - Method doesn't seem to work?
Woops, I meant initialize a = 0 not sure how I managed to mess that up.
Ok its starting to make sense a bit more now.
I commented out int b = 11; in methodOne(); and you were right the answer does change to b = 20.
I think I am beginning to understand now:
so methodOne() sets a = 10 because of the assignment statement in that method.
methodOne() creates a local variable for b which is b = 11.
But because this is a local variable, it means it is only available in the scope of methodOne();
So as soon as methodOne() has been invoked, the local variable b doesn't exist anymore, hence why the value of b in the class object doesn't change.
Am I correct so far?
Am I also correct in saying that when the methodOne() is invoked, Local variable called b is created, with a value of 11, then the assignment statement 2 lines down changes the value of this variable to 20, then once the method is completed, the local variable ceases to exist (which is why b will not = 20 and will revert back to the instance variable value of 10).
Is this all right? Please tell me it is!
- 01-16-2013, 03:06 AM #10
Re: Overloaded Variables - Method doesn't seem to work?
Sounds right.
It was never changed so it won't "revert back".will revert back to the instance variable value of 10).If you don't understand my response, don't ignore it, ask a question.
- 01-16-2013, 03:18 AM #11
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Variables in MainMethod(). Local or Instance.
By Flamey in forum New To JavaReplies: 3Last Post: 06-10-2011, 06:46 AM -
Are Local variables thread safe ?
By samson in forum Threads and SynchronizationReplies: 6Last Post: 12-21-2010, 02:34 PM -
Local variables unavailable
By Onra in forum New To JavaReplies: 9Last Post: 11-25-2009, 09:39 PM -
Re-docking the Local Variables window
By ScottVal in forum NetBeansReplies: 0Last Post: 01-20-2009, 07:59 AM -
Local Variables for a static method - thread safe?
By mikeg1z in forum Advanced JavaReplies: 1Last Post: 11-16-2007, 01:06 AM


4Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks