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 5 of 5

Thread: Very simple problem...PLEASE HELP!

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

    Default Very simple problem...PLEASE HELP!

    Consider the following code which is supposed to add a value passed to the method to an instance variable; however, there is a problem. Re-write this code, without renaming any variable names, to resolve the problem.

    private int number = 123;
    public void addNumbers (int number)
    {
    number = number + number;
    System.out.println("The local variable is: " + number);
    System.out.println("The instance variable is: " + number);
    }
    I have no idea where to even start... any ideas?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Very simple problem...PLEASE HELP!


    I have no idea where to even start... any ideas?
    Yes, write a runnable piece of code and test it...check the output, what you expect and what you get. Then fix it (hint: two variables have identical names - you must differentiate between the two).

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

    Default Re: Very simple problem...PLEASE HELP!

    I came with moving the print statement to the front so I have this:

    private int number = 123;
    public void addNumbers (int number)
    {
    System.out.println("The local variable is: " + number);
    number = number + number;
    System.out.println("The instance variable is: " + number);
    }
    Which takes care of the local variable. My problem is finding a way to differentiate between the two number variables, do you have any advice?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Very simple problem...PLEASE HELP!

    Read through the Oracle tutorials, the answer is in this link (and yes, there is a clue in this post)

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Very simple problem...PLEASE HELP!

    Quote Originally Posted by copeg View Post
    Read through the Oracle tutorials, the answer is in this link (and yes, there is a clue in this post)
    this will help you.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Simple problem...
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 5th, 2011, 11:02 PM
  2. Simple beginers problem
    By vycka1990 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 18th, 2010, 11:00 AM
  3. Simple Actionlistener problem
    By olemagro in forum AWT / Java Swing
    Replies: 5
    Last Post: October 11th, 2010, 10:46 AM
  4. Simple import problem
    By Mekster in forum Java IDEs
    Replies: 3
    Last Post: November 13th, 2009, 08:17 PM
  5. Simple scanner problem
    By Sinensis in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 20th, 2009, 09:01 PM