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: unexpected results to simple program

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    Southern Maryland
    Posts
    7
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default unexpected results to simple program

    I got this off af a tutorials page. For some reason one of the values is wrong or unexpected.
    public class IdentifyMyParts {
        public static int x = 7; 
        public int y = 3; 
     
     
    public static void main(String[] args) {
     
      IdentifyMyParts a = new IdentifyMyParts();
      IdentifyMyParts b = new IdentifyMyParts();
      a.y = 5;
      b.y = 6;
      a.x = 1;
      b.x = 2;
      System.out.println("a.y = " + a.y);
      System.out.println("b.y = " + b.y);
      System.out.println("a.x = " + a.x);
      System.out.println("b.x = " + b.x);
      System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);
     
    }
     
    }

    the output:
    jay@linux-9mi3:~/programming> java IdentifyMyParts
    a.y = 5
    b.y = 6
    a.x = 2
    b.x = 2
    IdentifyMyParts.x = 2


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

    Default Re: unexpected results to simple program

    The reason I ask is because your output looks completely correct and expected to me. I wonder if you are confused as to the concept of static fields, but I have to await your answer to know with certainty. Know this though, static fields are not fields of the object, but rather fields of the *class*. So if you change the value of a static field for an object, you change it for all. It is for this reason that you shouldn't try to access static fields on instance variables but rather on the class itself. It's much more clear doing it this way.

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

    Default Re: unexpected results to simple program

    What part of your output is "wrong or unexpected"?

    --- Update ---

    The reason I ask is because your output looks completely correct and expected to me. I wonder if you are confused as to the concept of static fields, but I have to await your answer to know with certainty. Know this though, static fields are not fields of the object, but rather fields of the *class*. So if you change the value of a static field for an object, you change it for all. It is for this reason that you shouldn't try to access static fields on instance variables but rather on the class itself. It's much more clear doing it this way.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: unexpected results to simple program

    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: unexpected results to simple program

    Why did you abandon this thread?

Similar Threads

  1. Replies: 0
    Last Post: March 1st, 2013, 08:13 PM
  2. Unexpected Output Format
    By playinmyblues in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 13th, 2012, 06:56 PM
  3. Unexpected ArrayOutOfBoundsError
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 28th, 2010, 04:00 AM
  4. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  5. ArrayList Unexpected Return
    By Cammack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2010, 09:23 PM