I want to create a KeyValue class but in generic manner and this is what I've written:
public class KeyValue<T,E> { private T key; private E value; /** * @return the key */ public T getKey() { return key; } /** * @param key the key to set */ public void setKey(T key) { this.key = key; } /** * @return the value */ public E getValue() { return value; } /** * @param value the value to set */ public void setValue(E value) { this.value = value; } public KeyValue <T, E>(T k , E v) // I get compile error here { setKey(k); setValue(v); } }
the error says : "Syntax error on token ">", Identifier expected after this token"
how should I create a generic constructor in java then?
Constructors are similar to methods and just like generic methods we can also have generic constructors in Java though the class is non-generic. Since the method does not have return type for generic constructors the type parameter should be placed after the public keyword and before its (class) name.
A generic constructor is a constructor that has at least one parameter of a generic type. We'll see that generic constructors don't have to be in a generic class, and not all constructors in a generic class have to be generic.
Due to the above conflict, instantiating a generic array in java is not permitted.
In order to use a generic type we must provide one type argument per type parameter that was declared for the generic type. The type argument list is a comma separated list that is delimited by angle brackets and follows the type name. The result is a so-called parameterized type.
A generic constructor is a constructor that has at least one parameter of a generic type. We'll see that generic constructors don't have to be in a generic class, and not all constructors in a generic class have to be generic. 2. Non-Generic Class First, we have a simple class Entry, which is not a generic class:
Here are some noteworthy points with regards to writing generic classes in Java: T is just a name for a type parameter, like a variable name. That means you can use any name you like for the type parameter. However, there are some conventions for naming type parameters in Java: T for type; E for element; K for key; V for value, etc.
Although the Entry class isn't generic, it has a generic constructor, as it has a parameter element of type E. The generic type E is bounded and should implement both Rankable and Serializable interfaces. Now, let's have a look at the Rankable interface, which has one method:
Constructors are similar to methods and just like generic methods we can also have generic constructors in Java though the class is non-generic. Since the method does not have return type for generic constructors the type parameter should be placed after the public keyword and before its (class) name.
Write constructor exactly the same way you wrote other methods
public KeyValue(T k , E v) { setKey(k); setValue(v); }
You need to remove <T, E>
from the constructor's signature: it's already there implicitly.
public KeyValue(T k , E v) // No compile errors here :) { setKey(k); setValue(v); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With