Adding default method to java.lang.Cloneable

David Holmes david.holmes at oracle.com
Sat Mar 3 03:27:46 PST 2012


On 3/03/2012 12:57 AM, Rémi Forax wrote:
> I've tried to add a default method to java.lang.Cloneable
> in order to be able to call it on an array
> (all arrays inherits from Object and implements Cloneable and Serializable).

The reason arrays can act as-if they implement Cloneable and 
Serializable is precisely because there are no methods in those 
interfaces. I don't think it reasonable to try and add methods to those 
interfaces and expect it to work for arrays. Making that work is part of 
the VM not a matter of simple bytecode. Just like you can't take Object 
and add to it, recompile and expect it to work.

David

> But the VM doesn't like that :)
> I think it's a bug.
>
> Here is the code
> package java.lang;
>
> public interface Cloneable {
>     public String foo() default {
>       return "foo";
>     }
> }
>
> ---
>
> public class Main {
>     public static void main(String[] args) {
>       int[] array = new int[] {2, 4 };
>       System.out.println(array.foo());
>     }
> }
>
> ---
>
> Rémi
>
>
>
>


More information about the lambda-dev mailing list