Results 1 to 5 of 5
  1. #1
    Maverrique is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default New to Java, Line of code is stumping me

    Hello all, thanks in advance for helping me out!

    I'm reading Head First Java and an exercise is confusing me a little bit. The Original instructions of the exercise are irrelevant, however, the point is to be able to solve it with out just compiling the code and running it, which would just spit out the answer. I was unable to do this because a particular line in the code. Here is the original code, with one line added myself, which i've noted.

    Java Code:
    public class Mix4 {
      int counter = 0;
      public static void main(String [] args) {
        int count = 0;
        Mix4 [] m4a = new Mix4 [20];
        int x = 0;
        while (x < 9) {
    	m4a[x] = new Mix4();
    	m4a[x].counter = m4a[x].counter + 1;  //This is the line confusing me
    	count = count + 1;
    	count = count + m4a[x].maybeNew(x);
    	System.out.println(count + " " + m4a[x].counter); //This is the line I added
    	x = x + 1;
    	}
    	System.out.println(count + " " + m4a[1].counter);
    	}
    
        public int maybeNew(int index) {
        	if (index < 5) {
    	Mix4 m4 = new Mix4();
    	m4.counter = m4.counter + 1;
    	return 1;
    	}
    	return 0;
    	}
    }
    The output is
    2 1
    4 1
    6 1
    8 1
    10 1
    11 1
    12 1
    13 1
    14 1
    14 1

    The line I added was to see what the value of "m4a[x].counter" was at each position in the array, not just it's value at the 2nd position, as it is in the original code. What I'm confused about is why the the value of "m4a[x].counter" never changes, despite the the line "m4a[x].counter = m4a[x].counter + 1"

    Again, thanks in advance for the help =)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    You're initializing m4a[x] to a new Mix4 each time around, which presumably has a default counter value of 0. You then add 1 to that, which makes it 1 each time. What did you expect would happen?

  3. #3
    Maverrique is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Ok, that makes perfect sense. So, as I understand it now, the counter value doesn't carry over each time around, like the count value does, because it was defined outside the method?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Quote Originally Posted by Maverrique View Post
    Ok, that makes perfect sense. So, as I understand it now, the counter value doesn't carry over each time around, like the count value does, because it was defined outside the method?
    Pretty much. More accurately, it's because you're creating a new instance each iteration through the loop and then only adding one to it. I don't think that's what you want to be doing.

  5. #5
    Maverrique is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    The exercise gave different While and If conditions, and asked you to figure out what the output would be. So the program is doing what it is supposed to do, I just thought the counter value would increase with each pass. I see how it works now, thank you very much!

Similar Threads

  1. Compiling java code from the command line
    By alman9898 in forum New To Java
    Replies: 5
    Last Post: 10-13-2010, 03:35 AM
  2. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  3. can you tell me what this line mean in this code?
    By Laythe in forum Java Applets
    Replies: 5
    Last Post: 10-17-2009, 11:10 PM
  4. what does this line of code mean?
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 01-25-2009, 04:52 PM
  5. What does this line of code mean?
    By naxalyte in forum New To Java
    Replies: 1
    Last Post: 11-23-2007, 09:28 PM

Posting Permissions

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