What is meant by "Each of these static members exists once per enclosing type" for static members inside of a generic class on angelikalanger

Viewed 189

On the Java Generics FAQs - Angelika Langer site there is a section that is confusing me. Here's the excerpt:

"Can generic types have static members?

Yes

Generic types can have static members, including static fields, static methods and static nested types. Each of these static members exists once per enclosing type, that is, independently of the number of objects of the enclosing type and regardless of the number of instantiations of the generic type that may be used somewhere in the program. The name of the static member consists - as is usual for static members - of the scope (packages and enclosing type) and the member's name. If the enclosing type is generic, then the type in the scope qualification must be the raw type, not a parameterized type."

What does she mean by enclosing type? Is it the class the member belongs to? Ok, let's just assume that's what she means; now what does she mean when she says "Each of these static members exists once per enclosing type"? Is she considering that each parameterization of the generic class is it's own enclosing type of the static member? Because if that's what she's saying she's wrong, each parameterization of a generic class shares the same instantiation of static members (i.e. each parameterization does not get it's own instance of a static member, they share the same instances of each static member). Or am I misunderstanding what she means by enclosing type. Any help would be greatly appreciated!!

2 Answers

I think when she says it can exist once per enclosing type, she means each parameterization of a generic class shares the same instance of their respective static members. Does this sound right to anyone? This has been stumping me for a while, I just want to make sure I'm not missing anything.

Edit: added “each parameterization of a generic class shares the same instance of their respective static members" for clarity

I found this reference linked directly beneath her answer, helpful…

REFERENCES    How do I refer to static members of a generic or parameterized type?

In which she explains…

Using the raw type as the scope qualifier, instead of the any instantiation of the generic type.

If you refer to a static member of a generic type, the static member name must be preceded by the name of the enclosing scope, such as EnclosingType.StaticMember. In case of a generic enclosing type the question is: which instantiation of the generic type can or must be used as the scope qualifier?

The rule is that no instantiation can be used. The scope is qualified using the raw type. This is because there is only one instance of a static member per type.

Using your own experience…

…there is a section that is confusing me…

…to help make it more concrete/less abstract, consider these two generic types (enclosing types) with static members…1

public class MatthewS < AL > { 
    …
    public static int ONLY_ONE = 1;
}

public class MyConfusion< T extends MatthewS < … > >{

    private T stacker;
     
    public static void getOOBasics( MatthewS < … > learner ){ … } 
    … 
    public T conflateSomething( T confused ) { … }  
    … 
}

…
int onlyOne = MatthewS.ONLY_ONE;         // ok
int notTwo = MatthewS< Short >.ONLY_ONE; // error
int notThree = MatthewS< ? >.ONLY_ONE;   // error
…

What does she mean by enclosing type? Is it the class the member belongs to?

In my customized examples, the class MatthewSencloses“ the static member ONLY_ONE. The class MyConfusionencloses“ the static member getOOBasics( MatthewS < … > learner ).2

…what does she mean when she says "Each of these static members exists once per enclosing type"?…

What she means is…

««…there is only one unique MatthewS.ONLY_ONE, regardless of the number of objects of type MatthewS and regardless of the number of instantiations of the generic type MatthewS that may be used somewhere in the program…»»

Which is saying the exact opposite of…

…each parameterization of the generic class is it's own enclosing type of the static member?…

What she's saying is this is correct…

MyConfusion.getOOPBasics( null );   // ok

But this is wrong…

MyConfusion< Void >.getOOPBasics( null ); // error

And the reason it's wrong is because, as Langer says: „The rule is that no instantiation can be used.“ And MatthewS< Void > is an instantiation.

…Or am I misunderstanding what she means by enclosing type…

Going by your questions, and by you saying: „if that's what she's saying she's wrong“, then it sounds like you are. Yes.


1 Relating a newly learned concept to your individual experience, enhances long-term memory retention of the concept.
2 My code examples are original. But derived from code snippets found in the FAQ the asker's question links to.

Related