• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics with two types

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no way to do this. Why can't you have



And in addition, declare al to be List<UseT1> ?
 
deepashree deval
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to make it generic at the time of object creation not at the time of class declaration.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can replace
if(o instanceof UseT1)
with either
if (o.getClass() == t1.getClass())
or
if (t1.getClass().isAssignableFrom(o.getClass())
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic