Solved

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

Posted on 2011-09-20
5
203 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now