Java programming (static clauses and "class variables")

static clauses and classes

About the word static that precedes the method name of the main method. You can read it if you understand the standard output, how to write classes, and how to call them.

Class variables

Classes are usually instantiated and used. Let's try the following method.

public class Calculate {
 private int s = 3;
 public int multipleIntegerNumber(int num){
  s = num + s;
  s = num + s;
  return s;  
 }
}

Let's call this in the main method.

public class Mainmethod{
  public static void main(String[] args){
    
  int result; 
   
   Calculate calculate = new Calculate(); 
   result = calculate.multipleIntegerNumber(3);
  }
}

Then the variable result is 9. Let's change it as follows.

public class Calculate {
private static int s = 3;
	private int ns = 3;
	int[] r = new int[2];

	public int[] multipleIntegerNumber1(int num){
	 s = 2*num + s; // -----> A
	 ns = 2*num + ns; 

	 r[0] = s;
	 r[1] = ns;
	 return r;

	}
}

Let's call this class from the following main method.

public class Mainmethod{
  public static void main(String[] args){
    
   int[] result1;
   int[] result2;

   ClassValue calculate1 = new ClassValue();
   ClassValue calculate2 = new ClassValue();

   result1 = calculate1.multipleIntegerNumber1(3);// -------> ➀
   result2 = calculate2.multipleIntegerNumber1(3);// -------> ➁

  }
}

Now what about result1 and result2? Speaking from conclusion qiita 1(クラス変数1).PNG It will be. The value of resul2 [0] changes. That's because I named it static. When there is no static, the value of the variable is reset for each instance. However. The value of the variable is inherited even if the instance to which static is attached changes. In the case of this example, the variable ns was not attached as static at the time of declaration, so the value was reset in instances calculate1 and calculate2. However, since the variable s is attached as static at the time of declaration, the value was inherited in instances calculate1 and calculate2. At ➀, $ 2 {\ times} num (= 6) $ is added to $ s (= 3) $ to make $ 9 $. Furthermore, when ➁, $ 2 {\ times} num (= 6) $ is added to $ s (= 9) $ to make $ 15 $. By the way, it is called an instance variable or a class variable depending on whether or not it has a static clause.

Instance variables Class variables
The static clause at the time of declaration Do not attach Put on
Variable value Different for each instance Shared by all instances

Two methods of class variables

Finally, I will give an example of how to use another class variable. The following ClassValue2 class defines two instances, the static_count and the class variable instances_count. And every time the four methods defined in the class are called, one is added to the two variables. And the value is given by the confirmation method.

public class ClassValue2 {
	private int s = 3;
	private static int static_count = 0;
	private int instans_count = 0;

	public void add(int num){
		s = num + s;
		static_count = static_count + 1;
		instans_count = instans_count + 1;
	}

	public void substract(int num){
		s = -num + s;
		static_count = static_count + 1;
		instans_count = instans_count + 1;
	}

	public void multiple(int num){
		s = num*s;
		static_count = static_count + 1;
		instans_count = instans_count + 1;
	}
	public void division(int num){
		s = 1/num*s;
		static_count = static_count + 1;
		instans_count = instans_count + 1;
	}

	public void confirmation(){
		System.out.println("static_count:" + static_count);
		System.out.println("instans_count:" + instans_count);
		System.out.println("s:" + s);
	}

}

Let's call it like this: We are calling from 3 instances.

ClassValue2 classValue2_1 = new ClassValue2();
classValue2_1.add(3);
ClassValue2 classValue2_2 = new ClassValue2();
classValue2_2.substract(1);
ClassValue2 classValue2_3 = new ClassValue2();
classValue2_3.division(2);
classValue2_1.confirmation();
classValue2_2.confirmation();
classValue2_3.confirmation();

The result is as follows: qiita 1(クラス変数2).PNG In this way. The variable instance_count can count the number of calls from that instance, and the variable static_count can count the total number of calls from all instances.

Recommended Posts

Java programming (static clauses and "class variables")
Java programming (variables and data)
[Java] Differences between instance variables and class variables
Java programming (class method)
Java programming (class structure)
[Java] Variables and types
[Java] Stack area and static area
[Java] Difference between static final and final in member variables
Java class definition and instantiation
About Java class variables class methods
[Programming Complete] §4 Variables and constants
[Java Silver] What are class variables instance variables and local variables?
About Java static and non-static methods
Why are class variables needed? [Java]
Java Primer Series (Variables and Types)
StringBuffer and StringBuilder Class in Java
Java static
Docker terms and commands
[Java] Stack area and static area
About Java static and non-static methods
Processing speed with and without static
static
[Challenge Docker from 0] Overview and terms of Docker
Java programming (static clauses and "class variables")
Java starting from beginner, variables and types
[Java] Inheritance and structure of HttpServlet class
Java programming (classes and instances, main methods)
[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
Java static story
java Scanner class
java programming basics
java (abstract class)
[Swift vs Java] Let's understand static and final
[Processing x Java] Data type and object-oriented programming
[WIP] Java variables
[Java] Nested class
Java anonymous class
Check static and public behavior in Java methods
java Generic Programming
About Java class
Java and JavaScript
XXE and Java
Java and Swift comparison (3) Class implementation / Class inheritance / Class design
Ruby: Differences between class methods and instance methods, class variables and instance variables
[java] abstract class
[Java] Object class
Java local class
[Java] How to use Calendar class and Date class
Write a class in Kotlin and call it in Java
[Java] Refer to and set private variables with reflection
Use of Abstract Class and Interface properly in Java
[Introduction to Java] Variable scope (scope, local variables, instance variables, static variables)
[Java] Handling of character strings (String class and StringBuilder class)
Constraint programming in Java
About class division (Java)
Getters and setters (Java)
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
abstract (abstract class) and interface (interface)
Java inner class review
Java programming basics practice-array