• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding Generic Types  RSS feed

 
Siva Rsrk
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Every One,

When iam using Wrapper Classes,
i got following error ,
GenericEx1.java:6: cannot find symbol
symbol : class T
location: class GenericEx1
T it=obj.add(3,4);

Below is My Application

public class GenericEx1
{
public static void main(String[] args)
{
GenOne obj=new GenOne();
T it=obj.add(3,4);
System.out.println("i Val---"+it);
}

}
class GenOne<T>
{
public T add(T i, T j) {
System.out.println("i--"+i+"--j--"+j);
return i;
}
}



When i change my Application to like this

public class GenericEx1<T>
{
public static void main(String[] args)
{
GenOne obj=new GenOne();
T it=obj.add(3,4);
System.out.println("i Val---"+it);
}

}
class GenOne<T>
{
public T add(T i, T j) {
System.out.println("i--"+i+"--j--"+j);
return i;
}
}


This is gots following error

GenericEx1.java:6: non-static class T cannot be referenced from a static context

T it=obj.add(3,4);

Please any one can help me regarding this problem and how to use Wrappers in a very better way
 
Aurelian Tutuianu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In both cases T is a placeholder for a class type. What I mean is that T does not exist at all at runtime and when you run a generic class you should specify which is the T type at creation time.

In simple words you can thing at a generic class like an "unfinished" class (like abstract classes), the generic types like T in your case, is like a gap which you should fill at runtime. At execution time T should be transformed into a real, concrete class. They are not means to be executed "per se", like they are.

That should work
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!