Static vs Instance Variables: Difference?

2019-01-03 16:17发布

What is the difference between a static and instance variable. The following sentence is what I cant get:

In certain cases, only one copy of a particular variable should be shared by all objects of a class- here a static variable is used.
A static variable represents class wide info.All objects of a class share the same data.

I thought that instance vars were used class wide whereas static variables only had scope within their own methods?

9条回答
疯言疯语
2楼-- · 2019-01-03 17:05

Say there is a test class:

class Test{
public static int a = 5;
public int b = 10;
}
// here t1 and t2 will have a separate copy of b
// while they will have same copy of a.
Test t1 = new test(); 
Test t2 = new test();

You can access a static variable with it's class Name like this

Test.a = 1//some value But you can not access instance variable like this
System.out.println(t1.a);
System.out.println(t2.a);

In both cases output will be 1 as a is share by all instances of the test class. while the instance variable will each have separate copy of b (instance variable) So

 t1.b = 15 // will not be reflected in t2.
 System.out.println(t1.b); // this will print 15
 System.out.println(t2.b); / this will still print 10; 

Hope that explains your query.

查看更多
疯言疯语
3楼-- · 2019-01-03 17:05

Instance variables:

These variables belong to the instance of a class, thus an object. And every instance of that class (object) has it's own copy of that variable. Changes made to the variable don't reflect in other instances of that class.

public class Product {
    public int barcode;
}

Class variables:

These are also known as static member variables and there's only one copy of that variable that is shared with all instances of that class. If changes are made to that variable, all other instances will see the effect of the changes.

public class Product {
    public static int barcode;
}

Full example:

Instance variables:

public class Main {
    public static void main(String[] args) {

        Product prod1 = new Product();
        prod1.barcode = 123456;

        Product prod2 = new Product();
        prod2.barcode = 987654;

        System.out.println(prod1.barcode);
        System.out.println(prod2.barcode);
    }
}

public class Product {
    public int barcode;
}

The output will be:

123456
987654

Now, change the instance variable to a class variable by making it static:

Class variables:

public class Main {
    public static void main(String[] args) {

        Product prod1 = new Product();
        prod1.setBarcode(123456);
        Product prod2 = new Product();
        prod2.setBarcode(987654);

        System.out.println(prod1.getBarcode());
        System.out.println(prod2.getBarcode());
    }
}

public class Product {
    public static int barcode;

    public int getBarcode() {
        return barcode;
    }

    public void setBarcode(int value){
        barcode = value;
    }
}

I used non-static methods to get and set the value of Barcode to be able to call it from the object and not from the class.
The output will be following:

987654
987654

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-03 17:07

Class variables only have one copy that is shared by all the different objects of a class, whereas every object has it’s own personal copy of an instance variable. So, instance variables across different objects can have different values whereas class variables across different objects can have only one value.

查看更多
登录 后发表回答