static:用於屬性和方法
static修飾屬性:無論一個類生成多少對象,所有這些對象共用唯一一個靜態成員變量。一個對象對該靜態變量進行修改,其他對象對該靜態變量的值也隨之發生變化。可以通過類名.成員變量名的方式來使用它。
static修飾方法:靜態方法不能被重寫,只能被隱藏。子類只能繼承父類的靜態方法,不能重寫父類靜態方法,子類隱藏了父類的靜態方法。靜態方法可以包含靜態和非靜態方法,非靜態方法只能包含非靜態方法,不能包含靜態方法。
instance method:
An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.
static method:
If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.
The distinction between hiding a static method and overriding an instance method has important implications:
- The version of the overridden instance method that gets invoked is the one in the subclass.
- The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.
參考:http://docs.oracle.com/javase/tutorial/java/IandI/override.html
靜態代碼塊:類加載到Java虛擬機上會執行靜態代碼塊內容。先執行父類的靜態代碼塊,然后依次執行子類靜態代碼塊,最后在執行父類構造方法和子類構造方法。
類加載先執行靜態代碼塊,然后執行構造方法
不能在靜態方法中訪問非靜態成員變量。不能再靜態方法中使用this關鍵字
final修飾類:表示該類是最終類,不能被繼承。
final修飾方法:表示該方法是最終方法,不能被重寫。
final修飾屬性:表示該屬性不能被改寫。
final修飾原生類型時,原生類型的值不能發生變化。
final修飾引用類型時,表示該引用類型不能再指向其他對象,但引用對象的內容可以發生變化。
final成員變量賦值方式:
1. 聲明final成員變量時就賦初值。
2. 在所有的構造方法中賦值。