Forums Register Login

Regarding Generic Types

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
So I left, I came home, and I ate some pie. And then I read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 773 times.
Similar Threads
Sorting the String Saved in ArrayList
why even if i dont fetch the return value of the function, its not giving error?
lil confused!!
Regarding Wrapper Classes
Why is it not required to use the reference returned from the method.?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:42:29.