Please comment, vote on the idea and suggest on this sample code
//@Compiled with javac -version 1.6.0_01-ea
//@Executed with the details as below:
/*
java version "1.6.0_01-ea"
Java(TM) SE Runtime Environment (build 1.6.0_01-ea-b01)
Java HotSpot(TM) Client VM (build 1.6.0_01-ea-b01, mixed mode, sharing)
on Microsoft Windows XP PC over Intel Celeron Processor.
*/
class GenericTest{
publicstaticvoid main(String args[]){
//How to explicity specify the Float as U in constructor rather than recognizing by inference
//Something like the below commented statement is giving an error.
//AClass<Integer, String> aClass = new <Float>AClass<Integer, String>(30, "Hello World", 3.14);
AClass<Integer, String> aClass =new AClass<Integer, String>(30,"Hello World", 3.14);
//The explicit type parameter passing to method is expected
//to be as similar to the constructor call. Please refer to the comments near to the
//method defintion. The below commented statement is giving an error.
//aClass.showThis<Character, String>('?);
aClass.<Character, String>showThis('?);
aClass.display();
}
}
/*class AClass<S extends Integer,T> {*/
//JLS (Java Language Specification) says that S can be replaced by here Integer
//(as the super-class) or any class that is a sub-class of Integer
//where Integer is an inclusive upper limit.
//But here it is not working. We have to use Number which is one class level above.
class AClass<Sextends Number,T>{
S s;
T t;
public <U, V> AClass(S s, T t, U u){
this.s = s;
this.t = t;
/*V v = "One String";*/ V v =null;
System.out.println("Inside Constructor to display value - " + u);
System.out.println("Inside Constructor to display value - " + v);
}
//Generic method should look must be similar to generic class syntax construct
//The type parameter tag must come after the method name token as expected in the
//generic class syntax construct. But the below commented statement is giving an error.
//void showThis<P, Q extends String>(P p){
<P, Qextends String>void showThis(P p){
//JLS (Java Language Specification) doesn't state for cases where inference
//tries to happens in the method body. So the below commented statement is
//not getting inferred correctly, even though if we explicity specify the
//type parameter at the callee. This seems that the type parameter specification
//at the calle is no way giving any advantage by any chance.
/*Q q = "A String";*/ Q q =null;// Only null assignment allowed.
System.out.println("Showing This " + p);
System.out.println("showThis()::q=" + q);
}
void display(){
System.out.println("this.s = " + this.s +" this.t = " + this.t);
}
}

