LearnFrenzy
Sign in Take a free test

Object Class and Wrapper Classes · Find the Error · Question 5

Error: calling Object.clone() from another class Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

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

Main.java
class Config implements Cloneable {
    String env = "prod";
}

public class Main {
    public static void main(String[] args) throws Exception {
        Config c = new Config();
        Config copy = (Config) c.clone();
        System.out.println(copy.env);
    }
}
  • A Config must not implement Cloneable
  • B Object.clone() is protected, and Config does not override it publicly, so Main cannot call it on a Config Correct
  • C env must be final
  • D The cast is wrong
Correct Answer: Option B

javac: "clone() has protected access in Object". Override it as public in Config.

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