LearnFrenzy
Sign in Take a free test

Classes and Objects · Find the Error · Question 5

Error: inner-class object created without an outer object Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    class Helper {
        String help() { return "helping"; }
    }

    public static void main(String[] args) {
        Helper h = new Helper();
        System.out.println(h.help());
    }
}
  • A Helper must be public
  • B Helper is an inner (non-static) class, so it needs an enclosing Main object: new Main().new Helper() – or declare it static Correct
  • C help must be static
  • D Classes cannot be nested
Correct Answer: Option B

javac: "non-static variable this cannot be referenced from a static context".

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links