Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Homework Question

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Homework Question

    Consider the following code which is supposed to subtract a value passed to the method from an instance variable; however, there is a problem. Re-write this code, without renaming any variable names, to resolve the problem.
    private int number = 542;
    public void subtractNumbers (int number)
    {
    number = number - number;
    System.out.println("The local variable is: " + number);
    System.out.println("The instance variable is: " + number);
    }

    I don't need an answer to the problem, I just need to know where to start because I have no idea.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Homework Question

    You have two variables with the same name, one is a class field and the other a parameter to a method, and you must tell the compiler which is which. You can specify the class field by preceding it with something -- do you know what it is you can use?

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Homework Question

    At first I thought I should make it a constant but that didn't work...

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Homework Question

    Quote Originally Posted by EPC View Post
    At first I thought I should make it a constant but that didn't work...
    Hint #2, you would precede one of the variables with <something>.number. Please fill in the blank: What is <something>?

  5. The Following User Says Thank You to curmudgeon For This Useful Post:

    EPC (February 24th, 2013)

  6. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Homework Question

    I honestly have no idea.

    --- Update ---

    this?

    --- Update ---

    Thank you! But can you tell me what "this" does. I've gotten the correct answer but I've never seen "this" used before now.

  7. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Homework Question

    Quote Originally Posted by EPC View Post
    I honestly have no idea.

    --- Update ---

    this?

    --- Update ---

    Thank you! But can you tell me what "this" does. I've gotten the correct answer but I've never seen "this" used before now.
    this.<identifier_name> specifies that the identifier is a field of the class since it belongs to "this", to the instance of the class. That is why often you'll see setter methods that look like so:

    public void setFoo(int foo) {
       this.foo = foo;
    }

    The this.foo means a class field named "foo" is on the left hand side of the assignment while the parameter of the same name is on the right.

Similar Threads

  1. Question about homework problem.
    By Rain_Maker in forum Java Theory & Questions
    Replies: 13
    Last Post: February 7th, 2013, 08:11 PM
  2. Homework help?
    By regi.dg in forum Object Oriented Programming
    Replies: 0
    Last Post: October 16th, 2012, 09:17 PM
  3. Homework help
    By hockey15 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 24th, 2012, 12:28 AM
  4. Homework help
    By cd247 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2009, 06:56 PM
  5. need help with homework!
    By programmer12345 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 27th, 2009, 06:34 AM