Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a generic constructor for a generic class in java?

Tags:

java

generics

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?

like image 300
Seyed Vahid Hashemi Avatar asked Dec 30 '11 14:12

Seyed Vahid Hashemi


People also ask

Can generic class have constructor in Java?

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.

Can a generic class have constructor?

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.

Can you instantiate a generic class in Java?

Due to the above conflict, instantiating a generic array in java is not permitted.

How do you provide a parametrized type for a generic?

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.

What is a generic constructor in Java?

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:

How to write a generic class in Java?

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.

Does the entry class have a generic constructor?

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:

What is the difference between method and constructor in Java?

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.


2 Answers

Write constructor exactly the same way you wrote other methods

public KeyValue(T k , E v)      {         setKey(k);         setValue(v);     } 
like image 21
narek.gevorgyan Avatar answered Sep 29 '22 12:09

narek.gevorgyan


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); } 
like image 160
Sergey Kalinichenko Avatar answered Sep 29 '22 12:09

Sergey Kalinichenko