img
System.out.println("Cloning not allowed.");
return this;
}
}
}
class CloneDemo2 {
public static void main(String args[]) {
TestClone x1 = new TestClone();
TestClone x2;
x1.a = 10;
x1.b = 20.98;
// here, clone() is called directly.
x2 = (TestClone) x1.clone();
System.out.println("x1: " + x1.a + " " + x1.b);
System.out.println("x2: " + x2.a + " " + x2.b);
}
}
The side effects caused by cloning are sometimes difficult to see at first. It is easy to
think that a class is safe for cloning when it actually is not. In general, you should not
implement Cloneable for any class without good reason.
Class
Class encapsulates the run-time state of an object or interface. Objects of type Class are
created automatically, when classes are loaded. You cannot explicitly declare a Class object.
Generally, you obtain a Class object by calling the getClass( ) method defined by Object.
Class is a generic type that is declared as shown here:
class Class<T>
Here, T is the type of the class or interface represented. A sampling of commonly used methods
defined by Class is shown in Table 16-15.
Method
Description
static Class<?> forName(String name)
Returns a Class object given its complete name.
throws ClassNotFoundException
static Class<?> forName(String name,
Returns a Class object given its complete name.
boolean how,
The object is loaded using the loader specified by
ClassLoader ldr)
ldr. If how is true, the object is initialized;
throws ClassNotFoundException
other wise, it is not.
<A extends Annotation> A
Returns an Annotation object that contains the
getAnnotation(Class<A> annoType)
annotation associated with annoType for the
invoking object.
TABLE 16-15
A Sampling of Methods Defined by Class
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home