Class variables, class methods, instance variables, and instance methods in java
Before understanding the class variables and class methods, read the following code:
Class person {int age; string name; static int totalfee; public void showname () {system. out. print (this. name);} public static void showtotalloud () {system. out. print (total.pdf );}}
Three variables are defined in the person class. Two variables are the direct type + variable name, and the keyword static is added before the 3rd variables.
A class variable is also called a static variable, that is, a static variable is added before the variable;
Definition of class variables: access modifier static data type variable name
Instance variables are also called object variables, that is, variables without static;
So what are their differences?
Class variables are shared by all objects. One of the objects changes its value. Other objects get the changed result, while the instance variables belong
The object is private. An object changes its value without affecting other objects;
Also, both class variables and instance variables are global variables.
Two methods are also defined in the person class. The difference is that 2nd methods also have multiple static keywords.
This method is a class method. The class method is a class-related and public method.
The instance method belongs to each object.
Class method definition: access modifier static return data type method name (){}