LearnFrenzy
Sign in Take a free test

Classes and Objects · Find the Error · Question 2

Error: calling a method the class does not have Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

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

Main.java
class Lamp {
    boolean on;
    void switchOn() { on = true; }
}

public class Main {
    public static void main(String[] args) {
        Lamp l = new Lamp();
        l.switchOn();
        l.switchOff();
    }
}
  • A Lamp needs a constructor
  • B Lamp declares no switchOff() method, so javac cannot find the symbol Correct
  • C switchOn must return boolean
  • D l must be final
Correct Answer: Option B

Only methods declared in the class (or inherited) can be called.

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