LearnFrenzy
Sign in Take a free test

Classes and Objects · Find the Error · Question 4

Error: calling an instance method on the class name Answer, explanation & discussion

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

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

Main.java
class Calculator {
    int add(int a, int b) { return a + b; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(Calculator.add(2, 3));
    }
}
  • A add must return long
  • B add is an instance method, so it needs an object: new Calculator().add(2, 3) – or make add static Correct
  • C Calculator must be public
  • D 2 and 3 must be variables
Correct Answer: Option B

ClassName.method() works only for static methods.

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