LearnFrenzy
Sign in Take a free test

Constructors and the this Keyword · Find the Error · Question 6

Error: calling a private constructor from outside Answer, explanation & discussion

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

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

Main.java
class MathUtils {
    private MathUtils() { }
    static int square(int x) { return x * x; }
}

public class Main {
    public static void main(String[] args) {
        MathUtils m = new MathUtils();
        System.out.println(MathUtils.square(4));
    }
}
  • A square must not be static
  • B The constructor is private, so Main cannot create a MathUtils; call the static method on the class instead Correct
  • C Utility classes need public constructors
  • D square needs an object
Correct Answer: Option B

A private constructor deliberately prevents instantiation of a utility class.

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