静态
可以用私有的构造函数来限制非抽象类被初始化。
静态方法
静态方法是不知道堆上有哪些实例的。所以:
- 静态方法不能调用非静态的变量。
- 静态方法不能调用非静态的方法。
静态方法是不能被重写(override)的,它们只能被隐藏。
静态变量
- 静态变量被类的所有实例共享。
- 静态变量和实例变量一样有默认值。
- 静态变量会在该类的任何对象创建之前就完成初始化。
- 静态变量会在该类的任何静态方法执行之前就初始化。
静态初始化程序(静态初始化块)
A static initialization block is a normal block of code enclosed in braces, , and preceded by the static keyword. Here is an example:
static {
// whatever code is needed for initialization goes here
}
- A class can have any number of static initialization blocks, and they can appear anywhere in the class body.
- The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.
------ from Oracle Tutorial: Static Initialization Blocks
todo 静态方法和静态变量的初始化顺序
todo 静态方法和变量存在JVM的什么地方