[JAVA] Difference between instance variable and class variable

1.First of all

The difference between instance variables and class variables was ambiguous, so I summarized them for the future. I hope it helps.

2. What is an instance variable?

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).

3. What is a class variable?

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.

Postscript (2020/1/1) From @ shiracamus's comment

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.)

4. Each feature

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

5. Specific example

Instance variables

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.

Class variables

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.

bonus

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

Reference article

SOFTWARE ENGINEERING [tutorialspoint] (https://www.tutorialspoint.com/What-is-the-difference-between-class-variables-and-instance-variables-in-Java)

Recommended Posts

Difference between instance variable and class variable
Difference between class and instance
Difference between Ruby instance variable and local variable
Difference between variables and instance variables
Difference between interface and abstract class
[Java] Differences between instance variables and class variables
Difference between vh and%
Difference between i ++ and ++ i
Easy to understand the difference between Ruby instance method and class method.
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Java] Difference between "final variable" and "immutable object"
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between mockito-core and mockito-all
Difference between bundle and bundle install
Relationship between package and class
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
What is the difference between a class and a struct? ?? ??
Difference between Spring AOP and library proxy target class
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
[Ruby] Difference between get and post
Difference between render method and redirect_to
Difference between == operator and equals method
[Docker-compose] Difference between env_file and environment. Priority of environment variable application
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
[Rails] Difference between find and find_by
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between assignment of basic type variable and assignment of reference type variable
Differences between Applet class and JApplet class
[Java] Difference between array and ArrayList
What is the difference between an action and an instance method?
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
[Ruby] Maybe you don't really understand? [Difference between class and module]
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update
Difference between pop () and peek () in stack