The difference between instance variables and class variables was ambiguous, so I summarized them for the future. I hope it helps.
It is a variable that belongs to each instance. Specifically, the instance variable area (instance) is secured by the ** new keyword **, and the instance variable is initialized by the initial value assignment and the constructor (process executed when the instance is created). Note that each created instance is recognized as a separate instance, so the value of the instance variable will differ for each instance. Therefore, it is used when you want to handle different information for each object (instance of class).
A variable declared with the ** static keyword **, which is common information shared by all instances. Changing the value of a class variable from one object affects all other objects. Therefore, it is used when you want to handle information that is common to the entire object.
If there is an inheritance relationship between classes, class variables can have the same name for each inheriting class, and all inherited classes are traced and the first value reached is used. Therefore, even if a class variable has the same name, its value may differ depending on the referencing class. (Add a concrete example here to 5.)
Instance variables | Class variables | |
---|---|---|
Feature | An instance variable area is secured for each created object. Therefore, the value is different for each object. | Only one created for a class and common values are used for all objects |
Lifetime | From the time an object is created until it disappears | From program start to end |
How to call | Variable to which the object is assigned.Instance variables | 1.name of the class.Class variables 2.Variable to which the object is assigned.Instance variables |
Declaration | Access modifier type variable name | Access modifierstatic keywordType variable name |
Range of influence at the time of change | Only objects associated with instance variables | All objects |
Main.java
public class Main {
public static void main(String[] args) {
Sample sampleA = new Sample();
Sample sampleB = new Sample();
sampleA.a = 10;
sampleB.a = 20;
System.out.println(sampleA.a);
System.out.println(sampleB.a);
}
}
public class Sample {
public int a;
}
Output result
10
20
I set different values for the instance variables of each object, so As a result, different values are output.
Main.java
public class Main {
public static void main(String[] args) {
Sample sampleA = new Sample();
Sample sampleB = new Sample();
sampleA.setNum(30);
sampleB.setNum(40);
System.out.println(sampleA.getNum());
System.out.println(sampleB.getNum());
}
}
public class Sample {
public static int b;
public void setNum(int value) {
b = value;
}
public int getNum() {
return b;
}
}
Output result
40
40
Class variables share a common value across all instances, and
Because changes to one object affect other objects
As a result, the last set 40
is output as the value of the class variable.
Main.java
public class Main {
public static void main(String[] args) {
var s1 = new Sample();
var s2 = new Sample();
System.out.println("Base: " + Base.a + ", " + Base.b);
System.out.println("Sample: " + Sample.a + ", " + Sample.b);
System.out.println("s1: " + s1.a + ", " + s1.b);
System.out.println("s2: " + s2.a + ", " + s2.b);
Base.a = 10;
Sample.a = 20;
s1.a = 30;
s2.a = 40;
System.out.println("Base: " + Base.a + ", " + Base.b);
System.out.println("Sample: " + Sample.a + ", " + Sample.b);
System.out.println("s1: " + s1.a + ", " + s1.b);
System.out.println("s2: " + s2.a + ", " + s2.b);
Base.b = 100;
Sample.b = 999;
System.out.println("Base: " + Base.a + ", " + Base.b);
System.out.println("Sample: " + Sample.a + ", " + Sample.b);
System.out.println("s1: " + s1.a + ", " + s1.b);
System.out.println("s2: " + s2.a + ", " + s2.b);
}
}
class Base {
static int a = 0;
static int b = 1;
}
class Sample extends Base {
static int b = 2;
}
Output result
Base: 0, 1
Sample: 0, 2
s1: 0, 2
s2: 0, 2
Base: 40, 1
Sample: 40, 2
s1: 40, 2
s2: 40, 2
Base: 40, 100
Sample: 40, 999
s1: 40, 999
s2: 40, 999
SOFTWARE ENGINEERING [tutorialspoint] (https://www.tutorialspoint.com/What-is-the-difference-between-class-variables-and-instance-variables-in-Java)