Home >Java >javaTutorial >Java improvement (6) -----Keyword static

Java improvement (6) -----Keyword static

黄舟
黄舟Original
2017-02-09 13:40:27998browse

1. What does static represent?

There is no concept of global variables in Java, but we can implement a "pseudo-global" concept through static. In Java, static means "global" or "Static" means, used to modify member variables and member methods, of course, it can also modify code blocks.

Java divides memory into stack memory and heap memory. Stack memory is used to store some basic types of variables, arrays and object references, and heap memory mainly stores some objects. When the JVM loads a class, if the class has statically modified member variables and member methods, a fixed-size memory area will be opened for these member variables and member methods at a fixed location. With these "fixed" features , then the JVM can access them very conveniently. At the same time, if static member variables and member methods do not go out of scope, their handles will remain unchanged. At the same time, the concept of "static" contained in static means that it is irrecoverable, that is, if you modify it at that place, it will not change back to its original state. If you clean it up, it will not come back.

                                                                                                                                                                      The member variables and member methods being statically modified are independent of the class. It does not depend on a specific instance variable, which means that it is shared by all instances of the class. The references of all instances point to the same place, and modifications to any one instance will cause changes to other instances.

public class User {  
    private static int userNumber  = 0 ;  
      
    public User(){  
        userNumber ++;  
    }  
      
    public static void main(String[] args) {  
        User user1 = new User();  
        User user2 = new User();  
          
        System.out.println("user1 userNumber:" + User.userNumber);  
        System.out.println("user2 userNumber:" + User.userNumber);  
    }  
}      
------------  
Output:  
user1 userNumber:2  
user2 userNumber:2

2. How to use static

Static can be used to modify member variables and member methods. We call them static variables and static methods, which can be accessed directly through the class name.

        ClassName..propertyName

            ClassName.methodName(……)

##                                                                                                                                                                                                           Block code is very useful. (For the introduction of the use of code blocks in the past few days, please pay attention)

2.1. Static variables

Variables modified by static are called static variables, and variables that are not modified with static are called variables. are instance variables. The difference between them is:

The static variable is initialized when the class is loaded. It has only one in the memory, and the JVM will only allocate memory for it once. At the same time All instances of a class share static variables, which can be accessed directly through the class name.

But the instance variable is different. It is accompanied by the instance. Every time an instance is created, an instance variable is generated, which lives and dies with the instance.

So we generally use static variables in these two situations: data sharing between objects and easy access.

2.2. Static method

The static modified method is called a static method, and we call it directly through the class name. Since it exists when the class is loaded, it does not depend on any instance, so the static method must be implemented, which means that it cannot be an abstract method.

Static method is a special method in a class. We only declare methods as static when we really need them. For example, all methods of the Math class are static.

2.3. Static code block

The code block modified by static is called a static code block. The static code block will be executed as the class is loaded, and it can be placed at will. Can exist anywhere.


3. Limitations of Static

Static does have many functions, but it also has some flaws.

1. It can only call static variables.

2. It can only call static methods.

3. This and super cannot be quoted in any form.

4. Static variables must be initialized when they are defined, and the initialization time must be earlier than non-static variables.

Summary: Whether it is a variable, method, or code block, as long as it is modified with static, it will be "ready" when the class is loaded, that is, it can be used or has been executed, and can be detached. object and executed. On the contrary, if there is no static, it must depend on the object instance.

The above is the content of Java Improvement Chapter (6) ----- keyword static. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn