The article directories
- What problem does static solve
- format
- The characteristics of
- Static Static variable
-
- The difference between static and instance variables
- Static static method
- How to access static
- Static Usage precautions
What problem does static solve
When we create an object using the new keyword, the storage space of the data is allocated and the member methods of the class are used by the object. However, there are two special cases: 1. The objects created by the new keyword share the same resource, rather than each object has its own data, or there is no need to create the object, the resource and the object are unrelated. 2. You want a method not to be associated with any object of the class that contains it. Static allows you to use properties and call methods without creating objects
If a member variable is shared by all objects, the member variable should be defined as static
format
Modifier variable: Static data type variable name
Static method name (parameter list)
The characteristics of
- Static can modify variables, methods
- Variables or methods that are static are independent of any object of the class, that is, they do not belong to any instance object, but are shared by instance objects of the class
- When the class is loaded, the static part is loaded (this is important).
- A static variable or method takes precedence over an object, meaning that when a class is loaded, it can be accessed even if no object has been created
Static Static variable
A member variable that is static is called a class variable. A member variable that is not static is called an instance variable, indicating that the variable belongs to an object
The difference between static and instance variables
Instance variables: Each time an object is created, it will allocate memory space for member variables for each object. Instance variables belong to instance objects. In memory, there are several copies of member variables when objects are created several times
Static variables: Static variables are classes that do not belong to any instance objects, so they have only one copy in memory. During class loading, the JVM allocates memory for static variables once
Static static method
Methods that are static are also called static methods, because static methods do not belong to any instance object, so you cannot use this inside a static method, because it does not belong to any object, let alone this
How to access static
Static variables:
Class name. Static variable
Static method:
Class name. Static method
Since static variables and methods do not belong to any instance object, it is strongly recommended not to access static variables or methods by means of objects
Static Usage precautions
- There is no this keyword in static methods because static is loaded with the class, whereas this is created with the object. Static takes precedence over objects
- Static can access static, but not non-static
- Non-static can access both non-static and static