Static members
- A static member of a class is often referred to as a class member because a static member is associated with a class, not an instance of the class. Non-static member variable methods are often referred to as instance members because non-static member variables and methods exist only in instances of the class
- Static member variables and methods cannot be accessed using references because references refer to instances of the class and do not require instances of the class to access static members. To access a static member to use the class name
- Static methods cannot access instance members
Static initialization blocks
A static initialization block is a set of statements that are executed when a class is loaded by the class loader of the JVM. The static initialization block acts by allowing the class to perform any required setup tasks that need to occur only once
static{ 语句出现在这里 主体}
Instantiating an initialization block
Like a static initialization block, it is executed once for each instantiation of the object in the class. Instantiating an initialization block before the constructor call executes
public class Test{ public Test(){构造函数} {实例初始化块} }
- Execution order: the corresponding constructor in the subclass is called, the call to super is executed, the control flow jumps to the corresponding parent class constructor, the parent class constructor jumps back to the subclass constructor, and before the statement executed by the super in the subclass constructor, the instantiation initialization block executes
Inner class
Internal classes can be divided into four types:
Static inner class You can directly create an object of a static inner class without creating an object of the outer class, you can also reference the static member variable and the static method of the external class, but you cannot reference the ordinary members of the outer class
public class Test{ class Outer{ class Inner{成员内部内} } }
A member inner class can invoke all members of an external class. However, you can only refer to external members after you have created an object of the outer class.
public class Test{ public void f(){ class Inner{成员内部内} } } }
In a local inner class, to use a local variable in the method it is in, you need to define the local variable as final
new类或者接口的名字(){ 匿名内部类的主体 }
The anonymous inner class is a special inner class that does not have a name. The definition of an anonymous inner class is merged with the creation of the object.
An anonymous inner class is a class that is defined for a unique object, and when we only need to create an object of a class and cannot use its name, an anonymous inner class can be used
Java Learning (vii)