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