nerdexam
Oracle

1Z0-819 · Question #92

public class Main { class Student { String classname; Student(String classname) { this.classname = classname; } } public static void main(String[] args) { Student student = new Student(); // line 1…

The correct answer is A. Move the entire Student class declaration to a separate Java file, Student.java. E. Change line 3 to static Student student. Two compilation errors stem from the same root cause: Student is a non-static inner class of Main, so it cannot be instantiated inside a static method (main) without an instance of the outer class. Why A works: Moving Student to its own Student.java file promotes it to a…

Java Object-Oriented Approach

Question

public class Main { class Student { String classname; Student(String classname) { this.classname = classname; } } public static void main(String[] args) { Student student = new Student(); // line 1 Student classname = new Student(); // line 2 this.classname = classname; // line 3 } public static void main(String[] args) { var student = new Student("Biology"); // line 3 } } Which two independent changes will make the Main class compile? (Choose two.)

Options

  • AMove the entire Student class declaration to a separate Java file, Student.java.
  • BChange line 2 to public Student(String classname).
  • CRemove this.classname = classname;
  • DChange line 3 to Student student = new Student("Biology").
  • EChange line 3 to static Student student;

How the community answered

(56 responses)
  • A
    79% (44)
  • B
    2% (1)
  • C
    14% (8)
  • D
    5% (3)

Explanation

Two compilation errors stem from the same root cause: Student is a non-static inner class of Main, so it cannot be instantiated inside a static method (main) without an instance of the outer class.

Why A works: Moving Student to its own Student.java file promotes it to a top-level class, removing the outer-class dependency entirely - new Student("Biology") then compiles without issue.

Why E works: Changing line 3 to static Student student; replaces the problematic instantiation with a simple static field declaration (defaulting to null) - no instantiation occurs, so the inner-class constraint is never triggered.

Why the distractors fail: B only changes the constructor's access modifier, which does nothing to resolve the static/non-static context mismatch. C removes a line inside the Student constructor body, which is valid code and irrelevant to the outer compilation error. D replaces var with an explicit type but still calls new Student("Biology") in a static context - identical error, different syntax.

Memory tip: Whenever you see a non-static inner class accessed from a static method, remember the mantra: "static methods have no this, inner classes need one." The fix is either escape the inner class (option A) or skip the instantiation altogether (option E).

Topics

#inner-classes#static-context#compilation-errors#object-instantiation

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice