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: static variable accessible in object b, not in object a?

  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 static variable accessible in object b, not in object a?

    Here's simple class that has two instance variables, one static int and one just int. Why am I getting this output?

    Code:
    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();
     
      System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);
     
     
      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);
     
    //now I will try to change the values:
      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);
     
     
    }
    }

    My ouput:
    > java IdentifyMyParts
    IdentifyMyParts.x = 7
    a.y = 3
    b.y = 3
    a.x = 7
    b.x = 7
    a.y = 5
    b.y = 6
    a.x = 2
    b.x = 2
    IdentifyMyParts.x = 2

    As you can see, I tried to change a.x to 1, but get 2.
    b.x changed.
    I thought static variables were inaccessible.
    Clarify please. Thanks


  2. #2
    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: static variable accessible in object b, not in object a?

    I thought static variables were inaccessible.
    private variables are inaccessible. static has nothing to do with being accessible.

    There is only one x variable. It will have the last value assigned to it.

    See: http://www.javaprogrammingforums.com...tml#post105288
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: static variable accessible in object b, not in object a?

    So why is the second a.x = 2? where did 2 come from?
    If I edit out the second b.x assignment, a.x = 1 which is correct. If I leave it unedited, a.x = 2 which is incorrect - it should = 1.

  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: static variable accessible in object b, not in object a?

    where did 2 come from?
    This statement:
     b.x = 2;
    Is executed AFTER this one:
     a.x = 1;

    The variable: x will have the value from the last assignment statement.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: static variable accessible in object b, not in object a?

    Quote Originally Posted by Norm View Post
    This statement:
     b.x = 2;
    Is executed AFTER this one:
     a.x = 1;

    The variable: x will have the value from the last assignment statement.
    Thanks.

Similar Threads

  1. Passing as a parameter, an object + another variable
    By goochasauras in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 11th, 2012, 11:13 PM
  2. Replies: 1
    Last Post: February 14th, 2012, 07:42 AM
  3. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  4. object not accessible
    By sheikhms60 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 22nd, 2010, 07:37 PM
  5. static final object
    By kalees in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2009, 12:29 PM