1

I am not able to figure out solution to the below problem :

Problem statement : There is a class (GenericObject) which is a generic class and should be able to accept any type of objects.

Now I want to create objects of this class through a factory class. This factory class should give me either single object or a list of objects.

Now, single generic object I am able to get for object of my choice but I am not able to figure out the same for list of generic objects.

Below is the sample code :

package com.ge.hc.lcs.axis.components.factory;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String args[])
    {
        GenericObject<Integer> genericObject=Factory.getSingleInstance();
        //below line does not compile
        List<GenericObject<Integer>> genericObjectList=Factory.getListOfInstance();
    }

}

class Factory{
    public static GenericObject getSingleInstance()
    {
        return new GenericObject<>();
    }

    public static List<GenericObject> getListOfInstance()
    {
        List<GenericObject> genericObjectList=new ArrayList<>();
        genericObjectList.add(new GenericObject<>());
        genericObjectList.add(new GenericObject<>());
        return genericObjectList;
    }
}

class GenericObject<T>{
    void add(T object)
    {
        System.out.println(object.getClass());
    }
}

Now, below line works fine :

GenericObject<Integer> genericObject=Factory.getSingleInstance();

But this does not :

//below line does not compile
        List<GenericObject<Integer>> genericObjectList=Factory.getListOfInstance();

How can I achieve this. Any sort of ideas or suggestions are appreciated.

Onki
  • 1,879
  • 6
  • 38
  • 58

3 Answers3

1

Since your GenericObject class uses generics, you must define these generics in the method signature, too:

static class Factory {
    public static <T> GenericObject<T> getSingleInstance() {
        ...
    }

    public static <T> List<GenericObject<T>> getListOfInstance() {
...
Torsten Fehre
  • 567
  • 2
  • 7
  • any thoughts on this works : GenericObject genericObject=Factory.getSingleInstance(); – Onki Aug 07 '18 at 16:50
  • and how the generic object type Integer is passed to these methods? – Onki Aug 07 '18 at 16:57
  • It is not, it is inferred by the compiler from required return type - not my type of solution but works. I would rather pass `Some.class` object as argument, – Antoniossss Aug 07 '18 at 17:03
  • @Antoniossss any example to show how passing some.class would be better solution. I am trying to pass some.class but since its done through factory, I am not able to figure out a proper solution. – Onki Aug 13 '18 at 05:51
0

First: Dont use raw types. What is a raw type and why shouldn't we use it?

A way to fix your code would be to introduce type paramters to the methods of the factory.

class Factory{
  public static <T> GenericObject<T> getSingleInstance()
  {
    return new GenericObject<T>();
  }

  public static <T> List<GenericObject<T>> getListOfInstance()
  {
    List<GenericObject<T> genericObjectList=new ArrayList<>();
    genericObjectList.add(new GenericObject<T>());
    genericObjectList.add(new GenericObject<T>());
    return genericObjectList;
  }
}
k5_
  • 5,450
  • 2
  • 19
  • 27
0

Use generic type

public static <T> List<GenericObject<T>> getListOfInstance()
{
    List<GenericObject<T>> genericObjectList=new ArrayList<>();
    genericObjectList.add(new GenericObject<T>());
    genericObjectList.add(new GenericObject<T>());
    return genericObjectList;
}
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • any thoughts on this works : GenericObject genericObject=Factory.getSingleInstance(); – Onki Aug 07 '18 at 16:50