How to use the static keyword in Java

        The static keyword in Java is mainly used for memory management. We can apply the static keyword in variables, methods, blocks and nested classes. The static keyword belongs to the class, not the instance of the class.
        Static can be:

  • variables (also known as class variables)
  • methods (also known as class methods)
  • code block
  • nested class

        modifier variable

        Variables modified by static are called static variables or class variables; variables not modified by static are called instance variables. There is only one copy of static variables in memory (saving memory), JVM allocates memory for static variables only once, and completes memory allocation for static variables (stored in the "method area") during the class loading process, which can be accessed directly by the class name (convenient ), and of course can also be accessed through objects (but this is not recommended). For instance variables, each time an instance of a class is created, memory is allocated for the instance variable. Instance variables can have multiple copies in memory without affecting each other (flexible).

        The initialization order of static member variables is initialized in the order of definition. Static cannot modify local variables. Even static methods cannot have static variables, and non-static methods cannot be referenced, because static modified variables or methods are loading classes. At this time, non-static variables, methods, etc. have not been loaded, and of course they cannot be referenced. However, non-static methods or classes can normally reference static variables or methods. Because non-static always comes after static.

        Static variables are not owned by a specific object of the class where they are located, but are common to all objects of the class. Static variables can be called by the object or directly by the class. Therefore, static variables are generally used when the following two functions need to be implemented: when they are shared among multiple objects, and when it is convenient to access variables. Static variables cannot be serialized regardless of whether they are modified by transient or not.

        Modification method

        A method modified by static is called a static method. Since a static method can be accessed without relying on any object, there is no this for a static method because it is not attached to any object. Since there is no object, let’s talk about it. Not on this anymore. And because of this feature, non-static member variables and non-static member methods of the class cannot be accessed in static methods, because non-static member methods/variables must depend on specific objects to be called. But it should be noted that although non-static member methods and non-static member variables cannot be accessed in static methods, static member methods/variables can be accessed in non-static member methods. Static methods are generally used in tool classes, and you can directly use the class name to call the tool method for use.

        There are a few things to keep in mind about static methods:

  • They can only call other static methods.
  • They can only access static data.
  • They cannot refer to this or super in any way.

        Decorative class

        A class modified by static is called a static inner class. Generally, a common class is not allowed to be declared as static, but it can be declared as static in the inner class. At this time, the outer class can directly call the inner class, because the static inner class is loaded at the same time as the outer class is loaded. So you can directly call the static inner class without instantiating the outer class. A static inner class can only access the static members of the outer class, and cannot directly access the instance variables or instance methods of the outer class.

        static block

        A static code block is also called a static code block. It is a static statement block in a class that is independent of class members. There can be more than one, and the location can be placed anywhere. It is not in any method body. These static code blocks will be executed when the JVM loads the class. If there are multiple static code blocks, the JVM will execute them (automatically) in the order in which they appear in the class. Each code block will only be executed once, so Saying that static blocks can be used to optimize program performance. When there are both static{} blocks and static variables in a class, the principle of "define first, execute first" is also followed.

        static package

        Using import static statement instead of import static import package is a new feature in JDK1.5, you can import everything in a class that is modified by static, including variables, constants, methods and inner classes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324886629&siteId=291194637
Recommended