<!-The Java version is 1.8.0_161. I used Sublime Text 3 as the editor. -> <!-I learned that there are two types of Java, class methods and instance methods. -> When I was just learning Java, "By instantiating a class, you can use the variables that the class has." I think that it is often taught.
However, if you write static when declaring a variable, you can use that variable without instantiating the class.
In this article, I'll show you why some variables can be used without instantiation.
Class variables are variables that can be used without instantiation. Class variables are useful when you need variables that are common to each instantiated and created object. Also, when using class variables
(name of the class).(Class variable name)
will do. It feels like it's a property of the class.
On the other hand, when using instance variables
(Instance name).(Instance variable name)
will do. It feels like it belongs to the instance.
For example, consider a Circle class that calculates the area of a circle when you enter a radius.
Each time you enter a radius, an object of type Circle will be created. Each object has its own radius as an instance variable. However, when counting the number of created objects, it is useful to have variables that belong to the class rather than instance variables that belong to each object.
Main.java
public class Main{
public static void main(String args[]){
for (int i = 0; i < args.length; i++){ //Enter radius in command line argument
double r = Double.parseDouble(args[i]);
Circle circle = new Circle(r); //Creation of Circle type object
System.out.println("Area : " + circle.getArea());
}
System.out.println("The number of Circle objects : " + Circle.numberOfCircles);
}
}
Circle.java
public class Circle{
private double radius; //Radius of circle
private double area; //Area of a circle
public static int numberOfCircles = 0; //Class variable (representing the number of Circle objects)
public Circle(double r){
radius = r;
numberOfCircles++; //Incremented each time a Circle type object is created
}
public double getArea() {
return Math.pow(radius, 2) * 3.14;
}
}
If you execute Main.java as follows, ...
java Main 3 4 5 6
The execution result is
Area : 28.26
Area : 50.24
Area : 78.5
Area : 113.04
The number of Circle objects : 4
It will be.
Class variables can be accessed in common by each instantiated object. I think it's hard to convey in the above-mentioned dull example. ..
We plan to make corrections and additions as needed. 2018/09/09: Added a method to return the area to the Circle class.
<!-Oracle's The Java TM </ sup> Tutorials states: Was being done. The Japanese translation is suspicious, but please forgive me ...
If multiple objects are created from the same class, each will have a different instance variable. In the case of the bicycle class, the speed at which the pedal is turned, the gear, the speed, etc. are listed as instance variables. Each bicycle-shaped object has its own value for these variables and is stored in a different memory address.
When you need a variable that is common to all objects, you can add the static modifier. Fields that have the static modifier added when they are declared are called static fields or class variables. These are associated with the class, not each object. (Omitted below)
I'm sorry, but since it seems to be long, I introduced only the beginning. ->
Recommended Posts