1. The difference with or without static modification
static modification | No static modification | Remarks | |
---|---|---|---|
Attribution | class | Single object | |
Attributes | Class attributes, class variables | Instance attributes, instance variables | |
method | Class method | Instance method | |
Call method | Class name.attribute; class name.method; object.attribute; object.method | Object. Property; Object. Method | When the object is created, the data storage space can be allocated before it can be called by the outside world |
2. Basic knowledge points
(1) Use
(1) Put the static keyword before the definition , you can set the field or method as a static variable or static method.
eg:
class Person{ static int age = 18; // is to define the field of age as a static variable Static void NewYear{ Person.age++;} } Person p1 = new Person(); Person p2 = new Person(); ( 2) Quoting static variables : can be directly referenced by class name: Person.age can be located by objects: p1.age, p2.age (3) Reference static method : can be class name. Method () can be directly referenced: Person.NewYear() can Object positioning: p1.NewYear(), p2.NewYear() (2) Storage space (1) Class variables In a class, member variables declared with static are called static member variables, which are public variables of the class. It is initialized when it is used once. For objects of this type, there is only one static member variable. String constants and static variables are stored in the data area. (2) Instance variables Personal understanding:
Static variable-Static variable: Just like a book in the library for students to circulate, if a person takes notes on the book, it will affect every student who looks at it later.
Non-static variable-member variable: It can be understood that every student has his own textbook in class, and everyone can take notes in his own textbook, which has no effect on other students.
(3) Application scenarios
When you encounter the following situations, you need to use static:
1. Just want to allocate a single storage space for a specific domain;
for example, sign in the system, define the static variable sumPerson, you can ++
//define the initial value to 0
public static sumPerson =0;
public void person (string name) { this.name = name; id = sumPerson++; } Then whenever person is called, sumPerson will perform +1 operation
2. Even if the object is not created, it can be called; (Non-static domains and methods must be associated with a specific object in order to access data or methods) .
3. Comparison of static and non-static instances
(1) Static variable instances
(1) Non-static variables
(2) Static variables
Object references of
static variables Class references of static variables
(2) Static method instances
(1) Non-static variables are
here specifically Refer to the static variable of Class A created above to illustrate the variables in different classes. Static variables can also be called by way of ClassName.VariableName.
(2) Static variables
(3) Static code block example
From the above, it can be seen that the static code block is executed when the class is loaded, that is, the static code has the highest execution priority.