Solved

What are the restrictions on static methods in a generic class?

Posted on 2011-09-20
5
206 Views
Last Modified: 2012-05-12
I'm trying to understand this paragraph in my book. I guess this means that in a class like MyClass<T>, a static method cannot refer to T. Is that correct? Why are other methods allowed to refer to T while static methods are not?

"In a generic class, static methods and fields cannot refer to the class's type variables, since, after erasure, there are no type variables. Further, since there is only one raw class, static fields are shared among the class's generic instantiations"
0
Comment
Question by:shampouya
  • 3
  • 2
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36568626

Read what I posted yesterday in your question about the erasure.
At runtime there is no parameter T in the class.
So no matter whit is T all classes conatining T will be lbaleed the same raw class
And static mnthod is a property of a raw class istself- not of the instance of a class
that's why the peoprty of calass itself cannot depebnd on T

In the inastance of a class T is defined before the object is created at ruyntime - but static method does not depend on the instance and
is created at the loading of the class - so uit is logical that it cannot depend on T
 
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36568656
This is from yesterday's question which has direct connection to this question:

The compiler translates generic and parameterized types by a technique called type erasure .  Basically, it elides all information related to type parameters and type arguments. For instance, the parameterized type List<String> is translated to type List , which is the so-called raw type .  The same happens for the parameterized type List<Long> ; it also appears as List in the bytecode.

After translation by type erasure, all information regarding type parameters and type arguments has disappeared. As a result, all instantiations of the same generic type share the same runtime type, namely the raw type.

Example (printing the runtime type of two parameterized types):

    System.out.println("runtime type of ArrayList<String>: "+ne w ArrayList<String>().getClass());
    System.out.println("runtime type of ArrayList<Long>  : "+new ArrayList<Long>().g etClass());

    prints:   runtime type of ArrayList<String> : class java.util. ArrayList
          runtime type of ArrayList<Long>   : class java.util. ArrayList


So this is all because static method depends only on class itslef not on its instance - and class itself on runtime
already does not know about any T - this type in the instance already resolved.
Well, it seems clear to me - ask questions if you have any doubts.
0
 

Author Comment

by:shampouya
ID: 36568728
Oh ok so here is my understanding:

The generic type T in MyClass<T> is relevant when instance methods of MyClass are called through instances of MyClass. But when static methods in MyClass are called, they cannot depend on T because static methods are not called through objects, and thus they only have access to the raw features of the class, not the generic features.

Is that correct?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36568763
Yes, I think your statemnt is correct.

Basically when JVM deals with the class - this class is alreday has dependency on
T resolved. and if T would be in the static method - then it would not be possible.

All this stuff about generics is on compiler level only - not on the runtime levbel - this explains
all these restrictions
0
 

Author Closing Comment

by:shampouya
ID: 36568885
thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 51
custom annotations 9 42
SHA2 certs for IIS AND Java? 2 113
printf performancy 11 67
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question