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

Thread: Beginner: Passing Objects to Methods

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Beginner: Passing Objects to Methods

    Hello everyone,

    The names free_spirit and am a new registered user on this site. I am also a new programmer to the Java language. There is a problem in a book I am learning from, Introduction to Java Programming and Data Structures by Daniel Liang (11th edition), that I'm having trouble with.
    public class Test {
        public static void main(String[] args) {
           Count myCount = new Count();
           int times = 0;
     
           for (int i = 0; i < 100; i++)
              increment(myCount, times);
     
           System.out.println("count is " + myCount.count);
           System.out.println("times is " + times);
         }
     
         public static void increment(Count c, int times) {
            c.count++;
            times++;
         }
      }
     
     class Count {
          public int count;
     
           public Count(int c) {
              count = c;
           }
     
           public Count() {
              count = 1;
            }
      }

    The output is:

    count 101
    times 0.

    I understand why the output for test is zero but not so much for count. It would be helpful if anyone can explain. Thank you!
    Last edited by free_spirit; March 9th, 2018 at 01:39 PM. Reason: Added code tags

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,399
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Beginner: Passing Objects to Methods

    Please wrap posted code in code tags. I did it this time.

    The output is:
    Please copy the contents of the output and paste it here so we can see exactly what was printed. The posted code would not print what was shown.

    output for test is zero but not so much for count
    The value of count was saved in the object. The value was changed in the increment method via the object that was passed to it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    free_spirit (March 9th, 2018)

  4. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Beginner: Passing Objects to Methods

    The output is

    count is 101
    times is 0

    When you say the value of count was saved in the object, is it the value 1 in the no args Count constructor or the other constructor?




    LOL never mind , I finally understood what you said. Thank you Norm.
    Last edited by free_spirit; March 9th, 2018 at 01:45 PM. Reason: Solved

Similar Threads

  1. Passing Objects to Constuctors
    By fahman_khan75@yahoo.com in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 20th, 2014, 07:32 PM
  2. [SOLVED] Passing Strings in methods
    By 13hartc in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 16th, 2014, 07:02 PM
  3. Methods and passing variables
    By bamxmejia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 5th, 2013, 09:09 PM
  4. Passing Arrays Between Methods
    By kigroy in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 10th, 2011, 10:10 PM
  5. Objects passing to JSP
    By ober0330 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 28th, 2011, 02:19 PM

Tags for this Thread