Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-20
5
Medium Priority
?
216 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 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

916 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