Results 1 to 11 of 11
Like Tree4Likes
  • 1 Post By JosAH
  • 1 Post By milovan
  • 1 Post By Norm
  • 1 Post By Norm

Thread: Overloaded Variables - Method doesn't seem to work?

  1. #1
    bbfunk is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default 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:
    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 );
    }
    }
    So basically I understand that the variables in methodOne are local variables and the ones declared in the top are instance variables.

    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 09:22 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    bbfunk likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    bbfunk is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Overloaded Variables - Method doesn't seem to work?

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    Hi,

    But this doesn't answer why the instance variable a equals 10 after the two methods are invoked?

    If what your saying is true, then a should = 0.
    But when I inspect the objects properties after running both methods, a = 10 (and b = 10)

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default 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.
    Java Code:
    public void methodOne() 
    { 
    int b = 11; // brand new, local variable with the same name as instance variable
    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.
    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
    Java Code:
    int b = 11;
    and you will see that method will change value of instance variable b to 20.
    bbfunk likes this.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Overloaded Variables - Method doesn't seem to work?

    then a should = 0.
    Where is the variable a defined?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    bbfunk is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Overloaded Variables - Method doesn't seem to work?

    Quote Originally Posted by milovan View Post
    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.
    Java Code:
    public void methodOne() 
    { 
    int b = 11; // brand new, local variable with the same name as instance variable
    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.
    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
    Java Code:
    int b = 11;
    and you will see that method will change value of instance variable b to 20.

    You see this is what I thought.

    But when I run the code and send the object the messages I get the answer:
    a = 10
    b = 10

    If the only available variable with name "a" is the instance variable, then why does the value of the object of "a" change to 10, and not stay at 0?
    Last edited by bbfunk; 01-16-2013 at 03:18 AM.

  7. #7
    bbfunk is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Overloaded Variables - Method doesn't seem to work?

    Quote Originally Posted by Norm View Post
    Where is the variable a defined?
    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 03:17 AM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Overloaded Variables - Method doesn't seem to work?

    why is "a" changing its value from 0 to 10 when I run methodOne()?
    Because of this assignment statement in methodOne:
    Java Code:
    a = 10;
    It sets a to 10.

    so each new object of the class should initialize a = 10.
    No, the following code sets a to 0 when a new instance of the class is created.
    Java Code:
    private int a = 0;
    Last edited by Norm; 01-16-2013 at 03:26 AM.
    bbfunk likes this.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    bbfunk is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default 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!

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Overloaded Variables - Method doesn't seem to work?

    Sounds right.
    will revert back to the instance variable value of 10).
    It was never changed so it won't "revert back".
    bbfunk likes this.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    bbfunk is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Overloaded Variables - Method doesn't seem to work?

    Awesome thanks!

Similar Threads

  1. Variables in MainMethod(). Local or Instance.
    By Flamey in forum New To Java
    Replies: 3
    Last Post: 06-10-2011, 07:46 AM
  2. Are Local variables thread safe ?
    By samson in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-21-2010, 03:34 PM
  3. Local variables unavailable
    By Onra in forum New To Java
    Replies: 9
    Last Post: 11-25-2009, 10:39 PM
  4. Re-docking the Local Variables window
    By ScottVal in forum NetBeans
    Replies: 0
    Last Post: 01-20-2009, 08:59 AM
  5. Local Variables for a static method - thread safe?
    By mikeg1z in forum Advanced Java
    Replies: 1
    Last Post: 11-16-2007, 02:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •