Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-20
5
Medium Priority
?
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

730 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