我試圖找到關於此特異性的答案,但找不到任何答案。只有Static關鍵字作爲標題的括號內的指令
我在下面的代碼中有一個括號內只有static
關鍵字作爲標題的指令。
我的確清楚它的功能,並可以像其他static
方法/變量一樣猜測它們的用途,但我無法給它起個名字。
它不是一個變量也不是一個方法(它不會返回任何東西,甚至不包括「void
」),並且肯定不是構造函數,因爲使用了static
關鍵字。
我們稱這種特殊的「方法」是什麼?
下面是代碼:
public class Test{
static {
System.out.println("What do we call this?");
}
public Test(){
System.out.println("Instance of Test created");
}
public static void main(String[] args) {
new TestSon().go();
}
public void go(){
System.out.println("Go method Test");
}
}
class TestSon extends Test{
static {
System.out.println("Same here...");
}
public TestSon(){
System.out.println("Instance of TestSon created");
}
@Override
public void go() {
System.out.println("Go method son");
}
}
輸出:
What do we call this?
Same here...
Instance of Test created
Instance of TestSon created
Go method son
感謝暱稱:)非常清楚 –