What is the use of cloneable interface in java?

后端 未结 5 584
别那么骄傲
别那么骄傲 2020-12-02 00:33

What is the use of implementing a cloneable interface as it is a marker interface?

I can always make a public Object clone() method in my class. What is the actual p

5条回答
  •  孤街浪徒
    2020-12-02 00:57

    The purpose is specified in the javadoc. It is to specify that cloning of an object of this type is allowed.

    If your class relies on the built-in implementation of clone() (provided by the Object.clone() method), then this marker interface enables field-by-field cloning. (If you call the built-in clone method on an object that doesn't implement Cloneable, you get a CloneNotSupportedException.)

提交回复
热议问题