A
allen
Hi,
I'm trying to write a wrapper class for an Object value using generics.
This is a basic example of my problem, my questions are the comments in
the code below:
class Wrapper<T> implements Cloneable
{
private T value = null;
public Wrapper<T> clone( ) throws CloneNotSupportedException
{
// In reality, I need to do some deep cloning of other fields not
// shown here, hence I need to cast the result of super.clone( ).
// How do I cast the following line correctly, to avoid warnings?
// This gives a warning:
Wrapper<T> clone = (Wrapper<T>) super.clone( );
// and this gives an error:
// Wrapper<T> clone = Wrapper<T>.class.cast( super.clone( ) );
return clone;
}
public T getValue( )
{
return value;
}
}
I'm trying to write a wrapper class for an Object value using generics.
This is a basic example of my problem, my questions are the comments in
the code below:
class Wrapper<T> implements Cloneable
{
private T value = null;
public Wrapper<T> clone( ) throws CloneNotSupportedException
{
// In reality, I need to do some deep cloning of other fields not
// shown here, hence I need to cast the result of super.clone( ).
// How do I cast the following line correctly, to avoid warnings?
// This gives a warning:
Wrapper<T> clone = (Wrapper<T>) super.clone( );
// and this gives an error:
// Wrapper<T> clone = Wrapper<T>.class.cast( super.clone( ) );
return clone;
}
public T getValue( )
{
return value;
}
}