Question 4
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
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!