i have some questions on generics
1. if List(?) is a reifiable then why List(? extends Object) is not a reifiable type ?? (even though Type Erasure will convert both into List(Object) according to me reifiable is the type which is completely represented at runtime i.e. no useful information is removed(type information)
2. what is the difference between these two generics method
one difference is that first one is the class method, i.e no class instance is needed to call that method. Is there any other difference in the working of these method ?
3. why static generic method cannot use Type parameter defined by class
my reason for this: since static method can be called even if there is no instance and if there is no instance of generic class, the type parameter are unknown... so they are not allowed
4. why Type parameter cant be static
my reason :a) since static variable are the class variable and get there value at compile time, but Type parameter are unknown at compiler ...so this is not allowed
b) since static varibale are class variable and the original variable itself is shared among all the instances(not copy), so for generics it will be difficult for the compiler to predict the type of variable if there are multiple instance ie. Gen<Integer> g=new Gen<>(12); Gen<Stringr> g=new Gen<>("hello"); so now it is impossible for compiler to know about the type of the variable obj .
5. it is valid but,
is not a valid class..why super is not allowed here ??
I think this post didn't get a reply because it is a bit overwhelming. It is five questions in one post. In the future, please create five threads - one per question. Anyway:
1) I don't know.
2) Just that one is static and one is an instance method. It is rare to have types declared on methods on an instance method. Usually they would be declared on the class itself.
3) Your reason is correct.
4) Because generics imply the type can vary. A static variable is shared by all instances and therefore needs to have one known type.
5) What use would you have to use super there?
For 5, to add to what Jeanne said, there is no value addition if you are using <T super Number>. Let's first see what is the advantage of having <T extends Number>
Here since you declared that <T extends Number>, in the method incrementAndPrint you'll know you are getting an object which IS-A Number. So you can call the intValue method on it which is part of Number class. But if you are allowed to use <T super Number>, then you'll not get any value addition:
In this case where <T super Number>, in the method incrementAndPrint you don't know if num is of type Number or Object. So you can only call methods of Object class on it. Thus declaring <T super Number> is the same as <? super Number>, because the only guarantee you'll have is that T IS-A java.lang.Object which is the case for ? as well.
As for checking the compiled code, you might try some decompiler but I'm not sure what exactly you want to see in the compiled code...