View the exhibit:



public class Student {

    public String name = "";
    public int age = 0;
    public String major = "Undeclared";
    public boolean fulltime = true;

    public void display() {
        System.out.println("Name: " + name + " Major: " + major);
    }

    public boolean isFullTime() {
        return fulltime;
    }
}

Which line of code initializes a student instance?

A.

Student student1;
B.
Student student1 = Student.new();
C.
Student student1 = new Student();
D.
Student student1 = Student();

題解

選項A,僅僅只是宣告出一個可以用來儲存String物件參考的student1變數。

選項B,如果是要將「new」當作方法使用,應該使用物件類別的「newInstance」方法,用法如下:

try {
    Student student1 = Student.class.newInstance();
} catch (InstantiationException ex) {

} catch (IllegalAccessException ex) {

}

選項C,正確實體化物件的方式。

選項D,少了「new」運算子。