Take the 2-minute tour ×

I am trying to clone a object of class Integer, which does implement the cloneable inteface.

Integer a = new Integer(4);  
Integer b = a.clone();

I know there are work arounds for this, but I must implement it like this. why I am getting this error = clone() has protected access in java.lang.Object

Why would it say this? Isn't the clone method a public abstract method of clonable interface, what does it have to do with object. Thanks in advance :-)

share|improve this question
You can't implement it like that, so it's pointless saying you must. –  EJP Apr 28 '11 at 23:27
add comment

6 Answers

up vote 7 down vote accepted

Sure, all methods in Object are inherited. The clone method however is protected, thus only accessible from within subclasses.

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

The reason why you can call clone() in most other circumstances is that the subclass "opens it up" by overriding it with the public access modifier.

share|improve this answer
So user created classes are not automatically a subclass of object? Even when I made my class extend object it still didn't work. :-/ –  rubixibuc Apr 28 '11 at 9:04
@rubixibuc, every class in Java is a subclass of Object (except Object of course). In order to expose the clone() method to clients, you have to declare it in your subclass with a more liberal access modifier, either public or package private (default). This is the meaning of @aioobe's last sentence. However, you cannot extend java.lang.Integer because it is a final class. Also note that java.lang.Integer does not implement Cloneable. –  rlibby Apr 28 '11 at 15:24
add comment

java.lang.Integers are immutable. There is no reason to clone one. If you're trying to waste memory, try Integer.valueOf(myInteger.intValue()).

share|improve this answer
+1: That won't always create a copy as small values from -128 to 127 are cached. You need to explicitly use new Integer(myInteger) –  Peter Lawrey Apr 28 '11 at 7:16
@Peter: "you need to" iff you are trying to waste memory ;-) But there are easier ways to waste memory. new Object[BIG_NUM] is my favourite. –  Joachim Sauer Apr 28 '11 at 8:55
Or even new long[Integer.MAX_VALUE] ;) Or you can go for broke Unsafe.getUnsafe().allocateMemory(Long.MAX_VALUE) –  Peter Lawrey Apr 28 '11 at 9:13
add comment

The error comes because clone method of java.lang.Object is not visible publicly.

share|improve this answer
add comment

First of all obtaining a copy through cloning i needed when you dont want others to change your copy. Now Integer is a wrapper class like String and Double that are Immutable. That means you cant change the internals of it. So as long as you have the original reference your object is intact.


Integer a = new Integer(4); // a referencing to this newly Created Integer
    b = a;  // b is referencing to..
    b = new Integer(8) // b no longer references to 4. 
                      //We changed the reference not the internals of it which is impossible.
share|improve this answer
add comment

As Amit and aioobe points out, Integer is immutable, so no need to clone it.

But to answer your question. The clone() method is not part of the the clonable interface see: http://download.oracle.com/javase/6/docs/api/java/lang/Cloneable.html as aioobe tells you.

The clone method is declared as protected in Object, and you must overwrite it with a public method in order to use it. See http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#clone()

In short the reason for this is, that in order to clone a object, it might or might not be necessary to do a "deep-clone" eg. clone fields and their sub elements. Or it might not make sense to clone an object, as the case is with Integer (because it's immutable)

share|improve this answer
add comment

It may be hard to believe but your error is right : clone() has protected access in java.lang.Object

share|improve this answer
add comment

Your Answer


