File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Generics with two types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics with two types" Watch "Generics with two types" New topic

Generics with two types

deepashree deval

Joined: Oct 20, 2008
Posts: 12
I have written the following code , to check what is the incoming object and add it in the collection but, i get an error given below

class Employee{
class BankAcount{
class GenericClass<UseT1,UseT2>{
UseT1 t1; UseT2 t2; ArrayList al;

GenericClass(UseT1 t1,UseT2 t2){
this.t1 = t1;
this.t2 = t2;
public UseT1 getT1() {
return t1;
public void setT1(UseT1 t1) {
this.t1 = t1;
public UseT2 getT2() {
return t2;
public void setT2(UseT2 t2) {
this.t2 = t2;
public void addObject(Object o){
if(o instanceof UseT1)
public class GenericTwoTypes {
public static void main(String[] args) {
GenericClass gc = new GenericClass<Employee, String>(new Employee(),new String());
gc.addObject(new Employee());

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot perform instanceof check against type parameter UseT1. Use instead its erasure Object since generic type information will be erased at runtime

at Generics.GenericClass.addObject(
at Generics.GenericTwoTypes.main(
Can anybody please tell me how to resolve it.....

Thanks and regards
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

There's no way to do this. Why can't you have

And in addition, declare al to be List<UseT1> ?

[Jess in Action][AskingGoodQuestions]
deepashree deval

Joined: Oct 20, 2008
Posts: 12
I want to check at runtime which is the object , if it is of either UseT1 or UseT2 only then i want it to add to the collection else , i want to throw an exception
Mudassir Rahat

Joined: Oct 14, 2006
Posts: 3
You have to make it generic at the time of object creation not at the time of class declaration.

Mudassir -- �Diplomacy is the art of saying "Nice doggie" until you can find a rock.�
Peter Lawrey
Ranch Hand

Joined: Dec 21, 2008
Posts: 62
You can replace
if(o instanceof UseT1)
with either
if (o.getClass() == t1.getClass())
if (t1.getClass().isAssignableFrom(o.getClass())
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

But only if t1 is not null.
And Class has a method called isInstance which is a bit more useful:
if (t1.getClass().isInstance(o))

deepashree, you should do a search on type erasure. It will show you why you can't use UseT1 in some ways, including in instanceof checks or in the creation of objects and arrays.
[ December 31, 2008: Message edited by: Rob Prime ]

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Generics with two types
It's not a secret anymore!