Java Reference
In-Depth Information
public void set(Object reference) {
this.ref = ref;
}
}
As a Java developer, you would agree that we write this kind of code when we do not know the type of the objects
that we have to deal with. The ObjectWrapper class can store a reference of any type in Java, such as String , Integer ,
Person , etc. How do you use the ObjectWrapper class? The following is one of the ways to use your Wrapper class:
ObjectWrapper stringWrapper = new ObjectWrapper("Hello");
stringWrapper.set("another string");
String myString =(String)stringWrapper.get();
There's one problem in the above code. Even though you knew that you stored (and wanted to) a String
in the stringWrapper object, you had to cast the return value of the get() method to a String type in (String)
stringWrapper.get() . Consider writing the following snippet of code:
ObjectWrapper stringWrapper = new ObjectWrapper("Hello");
stringWrapper.set(new Integer(101));
String myString =(String)stringWrapper.get();
The above snippet of code compiles fine. However, you get a runtime ClassCastException in the third statement
because you stored an Integer in the second statement and attempted to cast an Integer to String in the third
statement. First, it allowed you to store an Integer in stringWrapper . Second, it did not complain about the code in the
third statement because it had no knowledge of your intent that you only wanted to use a String with stringWrapper .
Java has made some progress with the way it helps developers write type-safe programs. Wouldn't it be nice if
the ObjectWrapper class had some way of letting you tell it that you want to use it only for a specific type, say, String
this time and Integer the next ? Your wish is fulfilled by generics in Java. It lets you specify a type parameter with
a type (class or interface). Such a type is called a generic type (more specifically generic class or generic interface).
The type parameter value could be specified when you declare a variable of the generic type and create an object of
your generic type. Let's rewrite the ObjectWrapper class to use generics. Call the new class simply Wrapper .
If a type accepts a parameter, you need to specify the parameter name (a valid identifier) in angle brackets (< >)
after the name of the type. You will use T as the parameter name.
public class Wrapper<T> {
}
It is an unwritten convention that parameter names are one character, and to use T to indicate that the parameter
is a type, E to indicate that the parameter is an element, K to indicate that the parameter is a key, and V to indicate that
the parameter is a value. In the previous example, you could have used any name for the type parameter, like so:
public class Wrapper<Hello> {
}
public class Wrapper<MyType> {
}
If you want to use more than one parameter for a type, they must be separated by a comma. The following
declaration for MyClass takes four parameters named T , U , V , and W :
public class MyClass<T, U, V, W> {
}
 
Search WWH ::




Custom Search