Home > Java > javaTutorial > Detailed introduction to the static keyword in java

Detailed introduction to the static keyword in java

零下一度
Release: 2017-06-25 11:03:52
Original
1737 people have browsed it

The static keyword in java has two main functions:

First: Allocate a single storage space for a specific data type or object, which is different from creating an object. The number doesn't matter.

Second, Realize a method or attribute to be associated with the class instead of the object

Simply put, in the Java language, static mainly has Usage situations in 5: member variables, member methods, code blocks, inner classes and static import packages.

Basic usage:

  1. static modification Member variable: This member variable belongs to the class variable and can be directly referenced through ClassName.attributeName without An instance of new class.

  2. static modificationMember method: This method belongs to the method of the class and can be directly referenced through ClassName.methodName without the need for a new instance of the class.

  3. static modificationCode block: Executed only once when the class is initialized, and the loading order is strictly in accordance with the definition order of static resources in the class; static A code block can assign values ​​to static variables defined after it, but cannot access them. ;Parent class code block->Subclass code block.

  4. static modificationInternal class: static cannot modify ordinary classes, but can only modify internal classes. How to create an internal class modified by static: new OuterClass.InnerClass().

  5. staticImport package: Syntax "import static java.lang.Math.*", so that Math can be used directly in the class Static methods in a class do not need to write the class name. Personally, I think it is more convenient when a certain class is frequently used, but it reduces readability. And it is not recommended to import *

The following are examples of various uses of static

static modified members Variables and member methods

 A few conclusions:

/**
 *
 * Created by ascend on 2017/6/9 13:51. */public class Test {public static void main(String[] args) throws ClassNotFoundException {
        Class.forName("net.liebao.test.A");
    }
}class A {static {
        System.out.println("A.static initializer");
    }public A() {
        System.out.println("A.A");
    }
}
Copy after login

Output:

A.static initializer
Copy after login

 Conclusion 1: Static resources belong to the class, but are independent of the class. Static resources are loaded when the class is initialized (earlier than new), For example, you can load the static resources of a class through: Class.forName("xxx"), but there is no new.

 Conclusion 2: Static resources (static member variables, static member methods, the same below) cannot access non- Static resources and non-static resources are generated when they are new, so they cannot be accessed. On the contrary, non-static resources can access static resources.

## Conclusion Three: Static resources can access static resources, but It should be noted that static resources can assign values ​​to static resources defined after it, but they cannot access static resources defined after it.

Another common use is to implement singleton static Pattern

We all know that the characteristic of the singleton pattern is that the class can only have one instance. In order to achieve this function, the constructor of the class must be hidden, that is, the constructor must be declared as private, and provides a method to create an object. Since the constructed object is declared as private, the outside world cannot directly create an object of this type. It can only obtain the object of the class through the method provided by the class. To achieve this purpose, the only way to achieve this purpose is to create The method of the object is declared static, and the program example is as follows:

class Singleton {private static Singleton instance = new Singleton();//私有化构造函数private Singleton(){}public static Singleton getInstance(){return instance;
    }
}  
Copy after login
The above singleton mode is the hungry man mode;

The above is the detailed content of Detailed introduction to the static keyword in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template