How to define Java class variables?
In Java, class variables are also known as static variables. They can be defined using the static keyword within the class. Class variables are visible throughout the entire class and can be shared by all objects of the class.
The syntax for defining class variables is as follows:
public class ClassName {
static dataType variableName;
}
dataType represents the data type, while variableName represents the variable name.
For example, defining a class variable representing the number of students.
public class Student {
static int studentCount;
}
When using this type of variable elsewhere, it can be accessed by the class name. variable name, such as:
Student.studentCount = 10;
System.out.println("学生人数:" + Student.studentCount);
The output shows: number of students: 10