1. Class variable
- Differences between class variables and instance variables (key points)
Variable category | Variable definition | Variable call | Variable value change |
---|---|---|---|
Class variable | Class is modified by static | Class name Variable name / object name Variable name | Class variable is shared by all objects. As long as its value is changed by one object, the value of other objects is the changed value |
Instance variable | Class is not modified by static | Object name Variable name | An instance variable is private to an object. If a value is changed by an object, the value will only work in the object. |
1.1. Introduction cases of class variables
-
Example: there are a group of children playing with snowmen. From time to time, new children join. How do you know how many people are playing now?
-
Idea:
- 1. Define a variable in the main count method;
- 2. When a child joins the game, count + +, and finally count records how many children play the game;
-
code
//1. Create class public class Child { private String name; public Child(String name){ this.name = name; } public void join(){ System.out.println(name + "Joined the game..."); } } //2. Create main method public class hello { public static void main(String[] args) { //Define a variable count to count how many children have joined the game int count = 0; Child child1 = new Child("Baigujing"); child1.join(); count++; Child child2 = new Child("bitch"); child2.join(); count++; Child child3 = new Child("rat spirits"); child3.join(); count++; System.out.println(count); } }
- Operation results
1.2 introduction to class variables
-
Suppose: an int count is designed to represent the total number of people. When we create another child, we will add 1 to the count, and the count is shared by all objects. It's ok
-
Problem analysis:
- 1. count is an object independent, very embarrassing;
- 2. It is troublesome to access count in the future, and OPP is not used;
- 3. Therefore, we introduce class variables / static variables;
-
Example: define a static variable count, which is a static variable: the biggest feature will be shared by all Child instance objects;
//1. Create Child class public class Child { //Define a variable count, which is a type variable (static variable) static //The biggest feature of this variable will be shared by all object instances of Child class public static int count = 0; private String name; public Child(String name){ this.name = name; } public void join(){ System.out.println(name + "Joined the game..."); } } //2. main method call public class hello { public static void main(String[] args) { //Define a variable count to count how many children have joined the game int count = 0; Child child1 = new Child("Baigujing"); child1.join(); //count++; child1.count++; Child child2 = new Child("bitch"); child2.join(); //count++; child2.count++; Child child3 = new Child("rat spirits"); child3.join(); //count++; child3.count++; System.out.println("child1 of count value" + child1.count); System.out.println("child1 of count value" + child2.count); System.out.println("child1 of count value" + child3.count); System.out.println("Child of count value" + Child.count); } }
- Operation results
1.3 details of class variables
-
Class variable: static variable / static attribute, which is a variable shared by all objects of this class;
- ① When any object of this class accesses it, it gets the same value;
- ② Similarly, when any object of this class modifies it, the same variable is modified.
- ③ Class variables are created with class loading, so they can be accessed even if no object instance is created.
-
Syntax format
Access modifier static Data type variable name;
- Access format
Class name.Class variable name
- example
//1. Create child class public class Child { //Define a variable count, which is a type variable (static variable) static //The biggest feature of this variable will be shared by all object instances of Child class public static int count = 0; private String name; public Child(String name){ this.name = name; } public void join(){ System.out.println(name + "Joined the game..."); } } //2. main method call public class hello { public static void main(String[] args) { System.out.println("Child of count value" + Child.count); } }
- Operation results
1.4 details of class variables
-
Usage scenario: we need all objects of a class to share a variable, and class variables can be considered;
-
example
//1. Create class public class Child { //Define a variable count, which is a type variable (static variable) static //The biggest feature of this variable will be shared by all object instances of Child class public static int count = 0; //Common attribute / common member variable / non static attribute / non static member variable private String name; public Child(String name){ this.name = name; } public void join(){ System.out.println(name + "Joined the game..."); } public String getName() { return name; } public void setName(String name) { this.name = name; } } //2. Call class variables and instance variables respectively public class main { public static void main(String[] args) { //Create instance Child child = new Child("Zhang San"); //Call class variable System.out.println("class Child of count value" + Child.count); //Call instance variable System.out.println("example child of count value" + child.getName()); } };
- Operation results
2. Class method
- Define format
Access modifier static Data return type method name(){ }
2.1 difference between class method and instance method
Method name | Method definition | Method call | describe |
---|---|---|---|
Class method | Method modified by static | Class name Method name / object name Method name | Keywords related to objects (super, this) cannot be used |
Example method | Methods not modified by static | Object name Method name | have access to |
- example
//1. Create a class and define static variables and static methods of the class class Student { //Define an ordinary member private String name; //Define a static variable to accumulate students' tuition fees private static double fee = 0; public Student(String name){ this.name = name; } //1. When a method is decorated with static, the method is static //2. Static methods can access static properties / variables public static void payFree(double fee){ Student.fee += fee; } public static void showFee(){ System.out.println("The total tuition fee is:" + Student.fee); } } //2. Create the main method and call the student class public class hello { public static void main(String[] args) { //1. Create two student objects to accumulate tuition fees Student tom = new Student("tom"); //2. Print out the cumulative situation of a student after payment tom.payFree(100); tom.showFee(); Student mary = new Student("mary"); //3. Print out the cumulative situation of two students after payment mary.payFree(200); mary.showFee(); } } }
- Operation results
2.2 usage scenarios of class methods
- ① When the method does not involve any object related members, the method design can be called static method to improve the development efficiency;
- ② In the actual program development, some general methods are often designed as static methods, so that we can call methods without creating objects;