LearnFrenzy
Sign in Take a free test

Static and Final Keywords · Find the Error · Question 1

Error: calling an instance method from main Answer, explanation & discussion

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

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

Main.java
public class Main {
    int square(int x) { return x * x; }

    public static void main(String[] args) {
        System.out.println(square(5));
    }
}
  • A square must return long
  • B square is an instance method and main is static; call it on an object (new Main().square(5)) or make it static Correct
  • C println cannot print int
  • D x must be final
Correct Answer: Option B

javac: "non-static method square(int) 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