Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

914 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

12 Experts available now in Live!

Get 1:1 Help Now