2

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!!

Matthew S.
  • 464
  • 2
  • 12

2 Answers2

1

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

Matthew S.
  • 464
  • 2
  • 12
  • 1
    try looking at https://stackoverflow.com/questions/16976164/java-static-class-singleton-with-generic ? that confirms your assumption. – Nathan Hughes Sep 20 '20 at 19:03
  • I thought this was what she was trying to say. So, just to confirm with you, my answer here is the correct interpretation of what she's saying? She's saying all the different enclosing types share the same instantiation of their respective static members? Sorry I tend to overthink things waaay too much and just want to make sure that's what she means – Matthew S. Sep 20 '20 at 19:12
  • i don't think the term is defined anywhere i can find but that seems like the right interpretation. – Nathan Hughes Sep 20 '20 at 19:16
  • I can't believe she phrased it like that, that's like the most confusing way humanely possible. Why not just say "each static member exists once per parameterization of the generic class" which in my opinion is still confusing and there's at least several other simpler ways this can be described. Thank you for your input! – Matthew S. Sep 20 '20 at 19:29
  • I just feel like I'm in the twilight right now, I actually can't believe she wrote it like that. Is it common to refer to different parameterizations as different enclosing types? I know they technically are, but I mean, who the hell describes it like that? – Matthew S. Sep 20 '20 at 19:31
-2

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.

deduper
  • 1,944
  • 9
  • 22
  • Did you delete your answer and then repost this? When I was reading her site I didn't realize she had her own glossary in which she uses terms in a contradictory way to how they're used in the java SE (e.g. "instantiation" and "parameterization" refer to separate concepts in the SE but in her glossary they are the same thing). This is why I was saying she was conflating terms, because she IS, but she clarifies the conflation in her glossary. – Matthew S. Oct 08 '20 at 18:54
  • Furthermore, my answer to this question is 100 percent correct. When she said enclosing type she meant parameterization and if you were thorough in your reading or reasoning you would have realized that in this section http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeParameters.html#FAQ401 on her website she says, and I quote "there is only one instance of a static field per parameterized type." The reason why she didn't say "parameterized type" instead of "enclosing type" to begin with is either because she's not good at English or is already so used to conflating terms. – Matthew S. Oct 08 '20 at 19:05
  • I downvoted this answer because its the same as the other answer you deleted, which was downvoted as well, and contains snide insults. Please refrain from insulting strngers over the internet, its not acceptable anywhere let alone on stack. – Matthew S. Oct 09 '20 at 13:23