1

I having one requirement where I may pass any pojo and do as an argument like this

public class Genericclass <pojo,dao>     {
//What I am expecting is when I call a method of this        

 class it should give me pojo object 
 public pojo getPojo(){
//create new object of the pojo
return pojo type OBJ;
}
Public dao getdao(){

//create new day OBJ return dao OBJ; } }

i may use this while calling any type of pojo ex
Public class Temp {

Psvm(string args[]){
Add(Generic class<emppojo.class,empdao.class>);

//emp or dept or what ever I may pass // I will do some business logic and create OBJ for

generic claa and I will use getpojo and getdao methods

for creating new objects and I will get dao object call a

method, Sorry if I did any coding mistake . give me a solution
based on my requirement } // thanks in advance

Sai
  • 57
  • 1
  • 8
  • I get it , rather writing in a void main method if I wrote add method in some class say x then what will be the method signature ; for ex – Sai May 26 '14 at 12:35

1 Answers1

0

Due to java type erasure, at runtime the generic class is not known. This means you have not access to the class of the generic type at runtime. The link of sp00m shows the work around: Pass in the class to your constructor or getPojo() method.

public pojo getPojo(Class pojoClass){
    return pojoClass.newInstance();
}
BetaRide
  • 16,207
  • 29
  • 99
  • 177
  • `at runtime the generic class is not known`, that is not entirely true, if you dig and dig deep, you can get to the generic type: http://stackoverflow.com/questions/1901164/get-type-of-a-generic-parameter-in-java-with-reflection – kajacx May 26 '14 at 10:27
  • As the last comment in the accepted answer states: This does not solve the problem, since there's no way to call `T.class`. – BetaRide May 26 '14 at 11:48