Java Enum clone() MethodThe clone() method of Enum class throws CloneNotSupportedException. This method ensures that enums cannot be cloned, which helps to maintain their "singleton" property. SyntaxOverrideThe clone() method of Enum class overrides the clone() method of class Object. Return ValueThe clone() method doesn't returns anything. ThrowsThe clone() method throws CloneNotSupportedException if the object's class does not support the Cloneable interface. Subclasses that overriding this method can also throw CloneNotSupportedException exception to show that an instance cannot be cloned. Example 1Test it NowOutput: People with Red as their favourite colour are generally wiser. Example 2Test it NowOutput: Maruti Car models till yet: MarutiI launched in 2010 year. MarutiII launched in 2013 year. MarutiIII launched in 2016 year.
Next TopicJava Enum
|