aspose file tools*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics with two types" Watch "Generics with two types" New topic
Author

Generics with two types

deepashree deval
Greenhorn

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)
al.add(o);
}
}
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(GenericTwoTypes.java:39)
at Generics.GenericTwoTypes.main(GenericTwoTypes.java:49)
Can anybody please tell me how to resolve it.....

Thanks and regards
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
Greenhorn

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
Greenhorn

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())
or
if (t1.getClass().isAssignableFrom(o.getClass())
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: Generics with two types