Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make constructors with different generics in java

I have a class and the following are its constructors

public Data(ArrayList<String> row){
}

public Data(ArrayList<Integer> row){
}

The above is giving me an error saying :

Method Data(ArrayListList) has the same erasure Data(ArrayList)
as another method in type Data.

I even tried doing :

public Data(ArrayList<?> rows) {

    if (rows.get(0) instanceof String) {

        initializeData(rows);

    }

}

But in the above, I then have to convert the whole ArrayList into a ArrayList. Which I think is in-efficient.

I am trying to instantiate a class either with ArrayList or with ArrayList (the user has the choice).

Data = new Data( new ArrayList< String >() );

Data = new Data( new ArrayLIst< Integer >() ); So both of the above should work. Based on the type of input parameter passed the class would then call different methods.

Is there are better to achieve what I want ?

Thank you in advance.

like image 242
user590849 Avatar asked Nov 08 '13 04:11

user590849


People also ask

Can you make a generic constructor?

As we all know that constructors are like methods but without return types. Like methods, constructors also can be generic. Even non-generic class can have generic constructors. Here is an example in which constructor of a non-generic class is defined as generic.

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 you have two different constructors in Java?

The technique of having two (or more) constructors in a class is known as constructor overloading. A class can have multiple constructors that differ in the number and/or type of their parameters. It's not, however, possible to have two constructors with the exact same parameters.

Can you name 3 types of constructors?

There are three types of constructors: Default, No-arg constructor and Parameterized.


2 Answers

Generics are implemented in Java by erasure. This means that the generic type information exists only at compile time and that the compiler alone has the responsibility for assuring type safety when using generics. When your program runs, the generic type information simply does not exist.

The runtime class literal for

List<String>

and for

List<Integer>

are both List.class.

This is why the compiler will not allow you to provide an overloaded constructor with the collections that differ only in their generic type parameters. From the perspective of the run time environment, both these parameter types are the same.

As a workaround, you can wrap each of the generic ArrayList types in its own distinct helper class. This will allow you to provide overloaded constructors that take each of the helper classes as their parameters.

like image 55
scottb Avatar answered Nov 14 '22 22:11

scottb


You cannot do that, the error is telling you that generics will be erased at compile time and you would end up with 2 constructors with the same signature.

A few options you can explore:

public Data(String[] row)
public Data(Integer[] row)

Or:

public class Data<T> {
    public Data(List<T> row);
}

new Data<String>(new ArrayList<String>())
like image 30
denis.solonenko Avatar answered Nov 14 '22 22:11

denis.solonenko