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,仅仅只是声明出一个可以用来保存 Student 对象参考的 student1 变量。
选项 B,如果是要将「new」当作方法使用,应该使用对象类别的「newInstance」方法,用法如下:
try {
Student student1 = Student.class.newInstance();
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
}
选项 C,正确实体化对象的方式。
选项 D,少了「new」操作符。
