The use of the static keyword for the Java Foundation

Source: Internet
Author: User

The static keyword in Java is primarily 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 to the instance of the class.
Static (statically) can be:

    • Variables (also known as class variables)
    • Methods (also known as class methods)
    • code block
    • Nested classes

Modifier variables

A variable that is modified by static is called a static variable or a class variable, and a variable that has no static modification is called an instance variable. Static variables have only one copy in memory (memory saving), the JVM allocates only one memory for static variables, and the memory allocation of static variables (stored in the "method area") in the process of class loading, which can be accessed directly (conveniently) by the class name and, of course, by objects (but this is not recommended). For instance variables, each time an instance of a class is created, the instance variable is allocated one memory, and the instance variable can have multiple copies in memory, with no effect (flexibility).

The initialization order of static member variables is initialized in the order defined, and static cannot modify local variables, even if static methods cannot have static variables, and cannot refer to non-static methods, because static modified variables or methods are loaded at the same time as the loaded class, while non-static variables, method is not yet loaded, and of course it cannot be quoted. However, a non-static method or class can normally refer to a static variable or method. Because non-static is always present after the static.

Static variables are not owned by a specific object of the class, but are common to all objects of the class, and static variables can be called by the object as well as directly by the class. Therefore, it is common to use static variables when you need to implement the following two functions: when sharing between multiple objects, convenient access to variables. Static variables cannot be serialized, whether or not they are transient decorated.

Modification methods

The static method, which is called statically, is not dependent on any object for access, so there is no this for static methods because it is not attached to any object, and since there are no objects, this is not the case. And because of this feature, non-static member variables and non-static member methods of the class cannot be accessed in a static method, because non-static member methods/variables must be dependent on a specific object to be able to be called. It is important to note, though, that non-static member methods and non-static member variables cannot be accessed in static methods, but static member methods/variables are accessible in non-static member methods. Static methods are generally used in tool classes, and can be used directly by invoking tool methods with the class name.

The following points need to be noted for static methods:

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

Modifier class

A class that is statically decorated is called a static inner class. Generally a normal class is not allowed to be declared static, but in the inner class can be declared as static, the external class can call the inner class directly, because the static inner class is loaded at the same time as the external class loaded, so do not have to instantiate the class can directly invoke static internal classes. Static inner classes can access only static members of external classes, and cannot directly access instance variables or instance methods of external classes.

Static block

A static block of code is also called a code block, which is an independent static block of statements in a class that is separate from a class member, can have more than one position, and it is not in any method body. When the JVM loads a class, it executes these static blocks of code, and if there are multiple static blocks of code, the JVM executes them sequentially (automatically) in the order in which they appear in the class, and each block is executed once, so that the static block can be used to optimize program performance. When a class has both a static{} block and a static variable, it also follows the "define first execution" principle.

Static Guide Package

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

The use of the static keyword for the Java Foundation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.