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

Thread: Isn't the result supposed to be 6?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    Gjilan
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Isn't the result supposed to be 6?

    public class Examp {
        public static void main(String[] args) {
           int i = 5;
           Examp examp_object = new Examp();
           examp_object.inc(i);
           System.out.println(i);
     
        }
        public void inc (int k) {
    	k++;
        }
     
    }

    For my surprise, the result is appearing to be 5. Why is that?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    49
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Isn't the result supposed to be 6?

    What do you mean by "result"?
    Remember that variables are passed to methods by value. The method inc can only change its copy of the variable, not the source of the value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Location
    Minnesota
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Isn't the result supposed to be 6?

    Quote Originally Posted by obel1x View Post
    public class Examp {
        public static void main(String[] args) {
           int i = 5;
           Examp examp_object = new Examp();
           examp_object.inc(i);
           System.out.println(i);
     
        }
        public void inc (int k) {
    	k++;
        }
     
    }

    For my surprise, the result is appearing to be 5. Why is that?
    I'm kind of struggling with something similar, but in this case, I think what is happening is that you are sending the argument of i to the method inc. The method is then updating a local variable of the object examp_object which is actually an instance of itself, so i never actually changes because when you call the object which is the main method you end up resetting i back to 5?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,292
    My Mood
    Mellow
    Thanks
    193
    Thanked 668 Times in 655 Posts

    Default Re: Isn't the result supposed to be 6?

    It's helpful in these little study programs to 1) make them as simple as possible, and 2) to over-instrument them to see what's going on. To simplify, eliminate the instance creation. It doesn't add to the study. To over-instrument, add a few print statements to check the values of variables of interest. In the end, something like:
    public class TestClass
    {
        public static void main(String[] args)
        {
           int i = 5;
           inc(i);
           System.out.println( "i in main() = " + i );
     
        } // end method main()
     
        public static void inc ( int k )
        {
            k++;
            System.out.println( "k in inc() = " + k );
     
        } // end method inc()
     
    } // end class TestClass

Similar Threads

  1. I'm supposed to see text in this window
    By Ypmaha in forum Object Oriented Programming
    Replies: 3
    Last Post: March 9th, 2012, 11:52 AM
  2. Can anybody help me fix this, what I am supposed to do is at the top
    By peplo1214 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 17th, 2012, 06:05 AM
  3. Not able to read from a textfiel the way it's supposed to.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 27th, 2012, 03:11 AM
  4. Not sure what I'm supposed to do...
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2011, 11:13 PM
  5. I don't understand what I'm supposed to do
    By dmcettrick in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2011, 09:34 AM