Why are class variables needed? [Java]

Introduction

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

What is a class variable?

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.

Variables that belong to the class

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.

Summary

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

Why are class variables needed? [Java]
About Java class variables class methods
Why design patterns are needed
[Java Silver] What are class variables instance variables and local variables?
Java array variables are reference types
Java local variables are thread safe
[Java] Differences between instance variables and class variables
Why does Java call a file a class?
Java programming (static clauses and "class variables")
[Java] Class inheritance
java Scanner class
Java HashMap class
java (abstract class)
[WIP] Java variables
[Java] Nested class
Java anonymous class
About Java class
[java] abstract class
[Java] Object class
Java local class
When there are environment variables in Java tests
What I learned in Java (Part 2) What are variables?
Why java allows final class A {abstract class B {}}
About class division (Java)
About Java StringBuilder class
[Java] About Singleton Class
Java inner class review
Java class type field
Java programming (class method)
About Java String class
6th class: Variables, method
Why preventDefault is needed
Java programming (class structure)
[Java] Variables and types
About java abstract class
Why the get method is needed in the Calendar class
If variables are no longer highlighted in Eclipse's Java editor
Java Object Serialization why & when
[Java] Integer wrapper class reference
Java memo (standard class) substring
Java tips --StaticUtility class modifier
Java inflexible String class substring
(Note) Java classes / variables / methods
Java memo (standard class) length
Java equals are pretty unpleasant
Java programming (variables and data)
[Implementation] Java Process class notes
About Java class loader types
How to use java class
[Java] Comparator of Collection class
Java class definition and instantiation
[Environment variables] What are rails environment variables?
Java learning memo (abstract class)
What are practically final variables?
What are Ruby class methods?
Summary of Java Math class
[Java] What is class inheritance?
How to use Java variables
Why implement an abstract class (Abstract)
[Java basics] What is Class?
What are Java metrics? _Memo_20200818