开发者

Sharing class attributes in Java

Let's consider this example :

public class Shared {

    private int attribute;

    public Shared() {}

    public void incrementAttribute(int i) {
            attribute += i;
    }

    public int getAttribute() {
            return attribute;
    }

    public static void main(String[] args) {
            Shared s1 = new Shared();
            Shared s2 = new Shared();

            s1.incrementAttribute(1);
            s2.incrementAttribute(1);

            s1.getAttribute();
            s2.getAttribute();
    }    
}

How can I开发者_JAVA百科 change this class to have 1 2 in output when calling getAttribute() and not 1 1

Something like a global variable, I tried the final keyword but I can't set something using a Method.


You need to make the attribute static.

private static int attribute;
        ^^^^^^

Members that are declared static will be shared between all instances of the class.


Also, for it to output 1 2 you need to change

s1.incrementAttribue(1);
s2.incrementAttribue(1);

System.out.println(s1.getAttribute());
System.out.println(s2.getAttribute());

to

s1.incrementAttribue(1);
System.out.println(s1.getAttribute());

s2.incrementAttribue(1);
System.out.println(s2.getAttribute());

ideone.com demonstration


Make the attribute variable static.


You actually hinted at the solution with your question title "Sharing class attributes in Java".

In your example attribute is an instance variable which means that each instance of your Shared class (s1 and s2) gets it own copy of the variable and they are updated independently.

What you need is a class variable i.e. something that is shared across all instances of a class. In Java this is also called a static variable. Add the static keyword to your declaration of the attribute variable to change it from an instance variable to a class variable:

public class Shared {
  private static int attribute;

  public Shared() {}

  public void incrementAttribute(int i) {
    attribute += i;
  }

  public int getAttribute() {
    return attribute;
  }

  public static void main(String[] args) {
    Shared s1 = new Shared();
    Shared s2 = new Shared();
    s1.incrementAttribute(1);
    System.out.println(s1.getAttribute());
    s2.incrementAttribute(1);
    System.out.println(s2.getAttribute());
  }
}

—Note that you also need to print out the value immediately after incrementing it, otherwise you get the output 2 2.


If you make the variable static it is shared by all instances of the class, which is what you require in your example. To give some clarification with regards to the final keyword that you tried - the final keyword is used to mark a variable that can only be initialized once. For a class member variable, such as the one you defined above:

private int attribute;

it must initialised either at declaration or within the class constructor - this is why you were unable to assign a value to it from within a method.


As your attribute is an instance field, each object you create will have an instance of attribute field with them. So when you are creating 2 objects s1 and s2 they both have individual attribute field.

If you want to have value shared by all the instances of the class, you need to have class level fields, for which you have to declare the attribute variable as static, and the value of it will depend on all the instance's call to increment method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜