147

It is commonly understood that Cloneable interface in Java is broken. There are many reasons for this, which I will not mention; others already did it. It is also the position of Java architects themselves.

My question is therefore: why has is not been deprecated yet? If the core Java team have decided that it is broken, then they must also have considered deprecation. What are their reasons against doing so (in Java 8 it is still not deprecated)?

32
  • 43
    This question is not "primarily opinion-based", as many apparently feel entitled to judge. Those who have nothing more than an opinion on the reasons are simply not qualified to answer. However, it is true that you only stand a remote chance of getting an authoritative answer here. It is also true that your question is not about a solvable problem you have, so it it's at least borderline off-topic. Commented Oct 16, 2014 at 7:53
  • 6
    @MarkoTopolnik I agree that there are some people out there in the world who could provide an authoritative answer, but I don't believe that's the test we apply here. The closure reason states "answers to this question will tend to be almost entirely based on opinions". I suspect that will be the case here, unless we get very lucky. Commented Oct 16, 2014 at 8:08
  • 2
    Here is "How and When" to deprecate from Oracle ... (docs.oracle.com/javase/6/docs/technotes/guides/javadoc/…) Cloneable interface might fall in the "buggy, or highly inefficient" case but its very open to opinions.
    – Maxx
    Commented Oct 16, 2014 at 8:10
  • 8
    @Duncan I still don't consider it fair to pass judgement on the question based on my assumptions about the lack of discipline on the answerers' part. If a user does not know the reason being asked about, (s)he is not entitled to abuse the answering facility to present his/her opinion on the matter. Commented Oct 16, 2014 at 8:14
  • 4
    @lexicore Yes, exactly---and you can bet they have thoroughly considered that option and by implication must have strong reasons not to deprecate it. Their own criticism of Cloneable is widely known. Commented Oct 16, 2014 at 8:32

3 Answers 3

124

There is a bug submitted in 1997 to Java Bug Database about adding clone() method to Cloneable, so it would no longer be useless. It was closed with resolution "won't fix" and justification was as follows:

Sun's Technical Review Committee (TRC) considered this issue at length and recommended against taking any action other than improving the documentation of the current Cloneable interface. Here is the full text of the recommendation:

The existing Java object cloning APIs are problematic. There is a protected "clone" method on java.lang.Object and there is an interface java.lang.Cloneable. The intention is that if a class wants to allow other people to clone it, then it should support the Cloneable interface and override the default protected clone method with a public clone method. Unfortunately, for reasons conveniently lost in the mists of time, the Cloneable interface does not define a clone method.

This combination results in a fair amount of confusion. Some classes claim to support Cloneable, but accidentally forget to support the clone method. Developers are confused about how Cloneable is supposed to work and what clone is supposed to do.

Unfortunately, adding a "clone" method to Cloneable would be an incompatible change. It won't break binary compatibility, but it will break source compatibility. Anecdotal evidence suggests that in practice there are a number of cases where classes support the Cloneable interface but fail to provide a public clone method. After discussion, TRC unanimously recommended that we should NOT modify the existing Cloneable interface, because of the compatibility impact.

An alternative proposal was to add a new interface java.lang.PubliclyCloneable to reflect the original intended purpose of Cloneable. By a 5 to 2 majority, TRC recommended against this. The main concern was that this would add yet more confusion (including spelling confusion!) to an already confused picture.

TRC unanimously recommended that we should add additional documentation to the existing Cloneable interface to better describe how it is intended to be used and to describe "best practices" for implementors.

So, although this is not directly about deprecated, the reason for not making Cloneable "deprecated" is that Technical Review Comitee decided that modifying existing documentation will be sufficient enough to make this interface useful. And so they did. Until Java 1.4, Cloneable was documented as follows:

A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.

Attempts to clone instances that do not implement the Cloneable interface result in the exception CloneNotSupportedException being thrown.

The interface Cloneable declares no methods.

Since Java 1.4 (which was released in February 2002) up to current edition (Java 8) it looks like this:

A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.

By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. See Object.clone() for details on overriding this method.

Note that this interface does not contain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. Even if the clone method is invoked reflectively, there is no guarantee that it will succeed.

12
  • 3
    and do you know why the clone method was in Object in the first place?
    – njzk2
    Commented Oct 16, 2014 at 17:40
  • 8
    @njzk2 That's an essential part of the mechanism---it's the method doing the low-level, extralinguistic magic of copying the object image bit for bit. Commented Oct 16, 2014 at 18:57
  • 3
    @Unheilig That magic is something you can't replicate with a copy constructor: Object#clone() produces an instance of the same class as the original without that class being known at compile time. Commented Oct 20, 2014 at 8:24
  • 4
    @AVolpe: That wouldn't fix the source incompatibility mentioned in the answer "where classes support the Cloneable interface but fail to provide a public clone method." In particular, classes providing a nonpublic clone method would be broken. Commented Oct 21, 2014 at 22:48
  • 2
    Nice history. Here's a direct link to the bug database. I've added some more history there, and I've quoted parts of it in my answer. Commented Oct 25, 2014 at 21:06
66
+100

The short answer to "why isn't Cloneable deprecated?" (or indeed, why isn't X deprecated, for any X) is that there hasn't been much attention paid to deprecating them.

Most things that have been deprecated recently were deprecated because there is a specific plan to remove them. For example, the addPropertyChangeListener and removePropertyChangeListener methods of LogManager were deprecated in Java SE 8 with the intention of removing them in Java SE 9. (The reason is that they unnecessarily complicated module interdependencies.) Indeed, these APIs have already been removed from early JDK 9 development builds. (Note that similar property change listener calls were also removed from Pack200; see JDK-8029806.)

No such similar plan exists to for Cloneable and Object.clone().

A longer answer would involve discussing further questions, such as what one might expect to happen to these APIs, what costs or benefits would accrue the platform if they were deprecated, and what is being communicated to developers when an API is deprecated. I explored this topic in my recent JavaOne talk, Debt and Deprecation. (Slides available at that link; video here.) It turns out that the JDK itself hasn't been very consistent in its usage of deprecation. It's been used to mean several different things, including for example,

  • This is dangerous and you should be aware of the risks of using it (example:Thread.stop(), Thread.resume(), and Thread.suspend()).

  • This is going to be removed in a future release

  • This is obsolete and it's a good idea for you to use something different (example: many of the methods in java.util.Date)

All of these are distinct meanings, and different subsets of them apply to different things that are deprecated. And some subset of them apply to things that aren't deprecated (but that maybe should be deprecated).

Cloneable and Object.clone() are "broken" in the sense that they have design flaws and are difficult to use correctly. However, clone() is still the best way to copy arrays, and cloning has some limited usefulness to make copies of instances of classes that are carefully implemented. Removing cloning would be an incompatible change that would break a lot of things. A cloning operation could be reimplemented a different way, but it would probably be slower than Object.clone().

However, for most things a copy constructor is preferable to cloning. So perhaps marking Cloneable as "obsolete" or "superseded" or something similar would be appropriate. This would tell developers that they probably want to look elsewhere, but it would not signal that the cloning mechanism might be removed in a future release. Unfortunately, no such marker exists.

As things stand, "deprecation" seems to imply eventual removal -- despite the fact that a vanishingly small number of deprecated features have ever been removed -- and so deprecation doesn't seem warranted for the cloning mechanism. Perhaps in the future an alternative marking can be applied that directs developers to use alternative mechanisms instead.

UPDATE

I've added some additional history to the bug report. Frank Yellin, an early JVM implementor and co-author of the JVM specification, made some comments in response to the "lost in the mists of time" comment in the TRC recommendation quoted in the other answer. I've quoted the relevant portions here; the full message is in the bug report.

Cloneable has no methods for the same reason that Serializable doesn't. Cloneable indicates a property of the class, rather than specifically saying anything about the methods that the class supported.

Prior to reflection, we needed a native method to make a shallow copy of an Object. Hence Object.clone() was born. It was also clear that many classes would want to override this method, and that not every class would want to be cloned. Hence Cloneable was born to indicate the programmer's intention.

So, in short. The purpose of Cloneable was not to indicate that you had a public clone() method. It was to indicate that you were willing to be cloned using Object.clone(), and it was up to the implementation to decide whether or not to make clone() public.

13
  • 3
    One fine answer you have here sir. I especially like that you don't just throw Object.clone() into the fire just because everyone else wants to, but you're willing to reason and bring up the good things of it.
    – icza
    Commented Oct 17, 2014 at 7:34
  • 2
    However, clone() is still the best way to copy arrays, and cloning has some limited usefulness to make copies of instances of classes that are carefully implemented. I was under the impression with the fix of 6428387, all code paths (clone, new/arrayCopy, Arrays.copyOf) resulted in the same intrinsics. Has anything changed recently?
    – bestsss
    Commented Oct 19, 2014 at 15:16
  • 2
    @bestsss I don't think array.clone() is necessarily any faster than any alternatives. From the API perspective it's the most concise way to duplicate an array. Arrays.copyOf(array, newlen) comes close, but it requires a length parameter, which is redundant if you're not changing the length. Commented Oct 20, 2014 at 23:30
  • 2
    @Holger Yes as far as we can see this is the first actual removal of an API since 1.1. Note also that even though we agree that Thread.suspend() and Thread.stop() (no-arg) are dangerous, they probably won't be removed -- or changed to throw an exception unconditionally -- because people actually use them! Presumably they're willing to bear the risk. One of the mitigating factors with the property change listeners is that they were used very rarely, so the impact of removing them is small. Commented Oct 20, 2014 at 23:37
  • 2
    @Holger Conceptually java.beans could be made independent of java.desktop since beans are just a library API for properties. Unfortunately if you dig into the beans API there are lots of dependencies on AWT. The implementation has even more. Certainly it might be possible to extricate them, but doing that seems like a lot more work than, say, disentagling logging from beans. The whole modularization effort is about doing this disentangling; undoubtedly more could be done, but then Jigsaw would take even longer. Commented Oct 23, 2014 at 20:25
-2

why it is not deprecated yet?

Because the JCP hasn't seen fit to do so, and may never do so. Ask them. You're asking in the wrong place.

What are the reasons behind keeping this thing in Java API

No-one will ever remove anything from the Java API, because of the backwards compatibility requirement. The last time that happened was the change in the AWT event model between 1.0 and 1.1 in 1996/7.

10
  • 17
    They did (effectively) remove Thread.stop(Throwable) by changing its contract to always throw UnsupportedOperationException to the caller (not to the target thread!). Commented Oct 16, 2014 at 9:03
  • 22
    What took place around the same time? The removal of Thread.stop(Throwable)'s functionality happened in Java 8. Anyway, the unqualified advice to "ask them" is wrong because today the chief Java architect himself is an active member on Stack Overflow. He just doesn't happen to bother answering anything but Streams-related questions. Commented Oct 16, 2014 at 9:16
  • 13
    Furthermore, OP's question is not about removal, but about deprecation, and clearly deprecation has been happening all along. Commented Oct 16, 2014 at 9:22
  • 18
    @EJP I'm not asking whether Cloneable will be removed from Java API. I'm asking why it's not going to be depracted. And I think this is a perfect place for asking.
    – Kao
    Commented Oct 16, 2014 at 9:22
  • 5
    @VaheHarutyunyan Thanks for the shout-out, but I'm not a Java architect. I'm an engineer in Oracle's JDK group, which maintains this stuff. Commented Oct 20, 2014 at 4:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.