aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question in generic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question in generic" Watch "question in generic" New topic
Author

question in generic

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
I have seen code like this in Sun tutorial

class Collections
{
public static <T> void copy(List<T> dest, List<? extends T> src){...}
}

but later, I saw this statement:

"what makes a class generic is the fact that it has the same behavior for all of its possible type parameters; the same class can be viewed as having many different types.
As consequence, the static variables and methods of a class are also shared among all the instances. That is why it is illegal to refer to the type parameters of a type declaration in a static method or initializer, or in the declaration or initializer of a static variable."

This statement seems saying its illegal to have generic type declaration in static method, but actually we can do that as shown in the example. Can someone correct my understanding on that statement above? Thanks.


SCJA, SCJP5.0, SCBCD, SCWCD
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
What it mean is -You can not do following:
<code>
If (myList instanceof ArrayList<String> // illegal.
</code>

This is illegal because class type iformation is same for all generic types <>.

Hope it helps.


Devender Thareja
SCEA, SCBCD, SCJP
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
Hi Devender, I know the example you posted. But this part still confuses me:

That is why it is illegal to refer to the type parameters of a type declaration in a static method or initializer, or in the declaration or initializer of a static variable

Can someone make examples on this statement?
eg. some illegal type parameter code in a static method making compilation error?

Thanks
Hamid Virani
Ranch Hand

Joined: Jul 26, 2005
Posts: 41
Hi Reubin ,

I think it is illegal to use the generic type in the static method which are defined at the class level. But the static method can define their own generic type.
So:

public class GenTest<T>{


//This will not work since it is using generic type defined in class
/*
public static void callMe(T obj) {
}
*/

//This will work since it defines it's own method
public static<V> void callMe(V obj) {
}


}

Hope this helps,

Thanks,

Hamid
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
Thanks Hamid, I see what it means now. I still have a question on this statement.


what makes a class generic is the fact that it has the same behavior for all of its possible type parameters; the same class can be viewed as having many different types.

As consequence ---- why ??

the static variables and methods of a class are also shared among all the instances. That is why it is illegal to refer to the type parameters of a type declaration in a static method or initializer, or in the declaration or initializer of a static variable.

Why is it a consequence inside?

My own guess is that the generic type passed in is unknown at runtime, thus its unknown which class's static member to call, so its not allowed. But for nonstatic members, they are called associated with class object, so to which class to call is determined.

Please judge my words
[ August 12, 2005: Message edited by: reubin yi ]
Sachin Ahuja
Greenhorn

Joined: Aug 12, 2005
Posts: 13
reubin,
You are just slighly mixing two things here:
There are two entities here
1. The generic class - for e.g. GenTest<T> (from Hamid Virani's post)
2. The actual type parameter, lets say String - for e.g.
GenTest<String> genTest = new GenTest<String>;

Now here's where you are getting a little confused:



My own guess is that the generic type passed in is unknown at runtime, thus its unknown which class's static member to call, so its not allowed. But for nonstatic members, they are called associated with class object, so to which class to call is determined.

Please judge my words

[ August 12, 2005: Message edited by: reubin yi ][/QB]


As you can see we will be calling static methods of GenTest and NOT String, so we know all along what static methods are we gonna call.
Now, Since in GenTest<T>, the class of <T> can change with every new instance, the static method method(T obj) can't know what class its gonna get ... and therefore its an illegal method declaration.

As mentioned earlier, the static method can have its own generic type though
public static <X> void print(X obj)

For a deeper understanding of wildcard instantiation, you can check my notes at http://www.sachinahuja.com/TigerNotesIndex.html

Hope this helps
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
Thanks Sachin. You are right. I just mixed the terms between generic type and type parameters.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: question in generic