[Java] Differences between instance variables and class variables

Purpose

To understand the difference between instance variables and class variables

Verification environment

Source code for verification

** 1. Class variables and instance variables **

qiita.java


public class Oimo {
	public static int CNT_CLASS; //Class variables
	public int CNT_INSTANCE; //Instance variables
}

** 2. Try class variables **

qiita.java


public class Kensho01 {

	public static void main(String[] args) {
		//Class variables
		Oimo oimo3 = new Oimo();
		Oimo oimo4 = new Oimo();

		oimo3.CNT_CLASS = 30;

		System.out.println(oimo4.CNT_CLASS); //30 is output
	}
}

** 3. Try instance variables **

qiita.java


public class Kensho01 {

	public static void main(String[] args) {
		//Instance variables
		Oimo oimo1 = new Oimo();
		Oimo oimo2 = new Oimo();

		oimo1.CNT_INSTANCE = 10;

		System.out.println(oimo2.CNT_INSTANCE); //0 is output
	}
}

Implementation procedure

--Prepare Oimo class with class variables and instance variables. Create two Oimo instances, set the values for the class variables and instance variables of the first instance, and then check the values of the class variables and instance variables set for the second Oimo instance.

Implementation results

--When I set the class variable of the first instance to 30, the class variable of the second instance was also set to 30. --When I set the instance variable of the first instance to 10, the instance variable of the second instance was set to 0.

Consideration

It turned out that instance variables are variables that are referenced for each instance, and class variables are variables that are commonly referenced by multiple instances. This knowledge is important if you want to write a thread-safe program.

See you again (^_^) Noshi

Recommended Posts

[Java] Differences between instance variables and class variables
Difference between variables and instance variables
Difference between class and instance
[Java Silver] What are class variables instance variables and local variables?
Difference between instance method and class method
Differences between "beginner" Java and Kotlin
Difference between instance variable and class variable
Differences between Applet class and JApplet class
Differences between Java and .NET Framework
Java programming (static clauses and "class variables")
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
[Java] Variables and types
Summarize the differences between C # and Java writing
[Java] Difference between == and equals
Java programming (variables and data)
[Java] Difference between static final and final in member variables
Java class definition and instantiation
Relationship between package and class
About Java class variables class methods
Differences between IndexOutOfBoundsException and ArrayIndexOutOfBoundsException
About instance variables and attr_ *
[Java Bronze learning] Differences between encapsulation, data hiding, and information hiding
Think about the differences between functions and methods (in Java)
Differences in how to handle strings between Java and Perl
[Java] A class is an OS, and an instance is a virtual computer.
Why are class variables needed? [Java]
Difference between interface and abstract class
[Java] Difference between Hashmap and HashTable
Java Primer Series (Variables and Types)
Organize classes, instances, and instance variables
StringBuffer and StringBuilder Class in Java
[JAVA] Difference between abstract and interface
[Java] Relationship between H2DB and JDBC
[Java] Difference between array and ArrayList
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
Easy to understand the difference between Ruby instance method and class method.
Conversion between Kotlin nullable and Java Optional
Java starting from beginner, variables and types
Relationship between kotlin and java access modifiers
Difference between final and Immutable in Java
Difference between Ruby instance variable and local variable
Differences between preface and postfix of operators
[Note] Cooperation between Java and DB (basic)
[Java] Instance method, instance field, class method, class field, constructor summary
Differences between Ruby strings and symbols [Beginner]
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
HashMap # putAll () behaves differently between Java 7 and Java 8
Activity transition with JAVA class refactoring and instance experimented on Android side
[Java] Understand the difference between List and Set
[Java] output, variables
Java class methods
Instance concept, class type variables, constructors, static members
Class and model
[Java] Class inheritance
[Java] How to use FileReader class and BufferedReader class
[Ruby] Basic knowledge of class instance variables, etc.
Java HashMap class