Which three statements are true about the structure of a Java class?



A. A class can have only one private constructor.
B. A method can have the same name as a field.
C. A class can have overloaded static methods.
D. A public class must have a main method.
E. The methods are mandatory components of a class.
F. The fields need not be initialized before use.

題解

選項A,一個類別可以擁有一個private修飾的私有建構子。這是對的,Java並沒有限制私有建構子的數量,舉例來說:

public class Test {

    private Test(){
        
    }
    
    private Test(int a){
        
    }
    
    private Test(String s){
        
    }
}

選項B,一個方法的名稱可以和欄位名稱相同。這是對的,舉例來說:

public class Test {

    int A;
    
    int A(){
        return A;
    }
}

選項C,一個類別可以擁有多載(overload)的靜態類別。這是對的,舉例來說:

public class Test {

    public static void test(String a, String b){
        
    }
    
    public static void test(int a, int b){
        
    }
}

選項D,一個公開的類別並不一定要有main方法。

選項E,並不是每個類別都需要撰寫方法,甚至類別內什麼都不寫也可以。

選項F,欄位並不是不用初始化,而是在宣告的時候就已經初始化了。