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

huangapple go评论63阅读模式
英文:

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

问题

Java Generics FAQs - Angelika Langer网站上有一个让我感到困惑的部分。以下是摘录:

> "泛型类型可以拥有静态成员吗?
>
> 是的
>
> 泛型类型可以拥有静态成员,包括静态字段、静态方法和静态嵌套类型。每个这些静态成员都针对封闭类型(enclosing type)存在,也就是独立于封闭类型的对象数量,而且与程序中可能使用的泛型类型的实例化次数无关。静态成员的名称由作用域(包和封闭类型)以及成员名称组成,这与静态成员通常的命名规则相同。如果封闭类型是泛型的,则作用域限定中的类型必须是裸类型(raw type),而不是参数化类型。"

她所说的封闭类型是什么意思呢?是指成员所属的类吗?好的,我们暂且假设是这个意思;那么她所说的"每个这些静态成员都针对封闭类型存在"是什么意思呢?她是不是认为每个泛型类的参数化都是静态成员自己的封闭类型?因为如果是这样,她是错误的,每个泛型类的参数化共享相同的静态成员实例(也就是说,每个参数化不会获得静态成员自己的实例,它们共享每个静态成员的相同实例)。或者是我误解了她所说的封闭类型的意思。任何帮助将不胜感激!

英文:

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

答案1

得分: 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

答案2

得分: -2

我在她的答案正下方直接找到了这个参考链接,有帮助...

>> 参考资料    如何引用泛型或参数化类型的静态成员?

在其中 她解释道...

>> … 使用原始类型作为范围限定符,而不是使用泛型类型的任何实例。
>>
>> 如果您引用泛型类型的静态成员,静态成员名称必须在封闭范围的名称之前,例如 EnclosingType.StaticMember。对于泛型封闭类型,问题是:哪个泛型类型的实例可以或必须用作范围限定符?
>>
>> 规则是不能使用任何实例化。范围使用原始类型限定。这是因为每种类型只有一个静态成员实例。
>> …

利用您自己的经验…

> „...有一部分让我感到困惑…

…为了使其更具体/不那么抽象,考虑这两个泛型类型(封闭类型),它们有静态成员...<sup>1</sup>

public class MatthewS &lt; AL &gt; { 
    
    public static int ONLY_ONE = 1;
}

public class MyConfusion&lt; T extends MatthewS &lt;  &gt; &gt;{

    private T stacker;
	 
    public static void getOOBasics( MatthewS &lt;  &gt; learner ){  } 
     
	public T conflateSomething( T confused ) {  }  
	 
}


int onlyOne = MatthewS.ONLY_ONE;         // ok
int notTwo = MatthewS&lt; Short &gt;.ONLY_ONE; // error
int notThree = MatthewS&lt; ? &gt;.ONLY_ONE;   // error

> „她所说的封闭类型是什么意思?是指成员所属的类吗?

在我的定制示例中,类*MatthewS* 封装了 静态成员 ONLY_ONE。类*MyConfusion* 封装了 静态成员 getOOBasics( MatthewS &lt; … &gt; learner )。<sup>2</sup>

> „...她说“每个这些静态成员每个封闭类型中只存在一次”是什么意思?…

她的意思是...

««…无论类型为MatthewS的对象数量有多少,无论在程序的哪里使用了MatthewS的泛型类型的实例化数量有多少都只存在一个唯一的 MatthewS.ONLY_ONE...»»

这是与...

> „...每个泛型类的参数化都是其自身静态成员的封闭类型吗?…

她说的是正确的…

MyConfusion.getOOPBasics( null );   // ok

但这是错误的…

MyConfusion&lt; Void &gt;.getOOPBasics( null ); // error

而这是错误的原因是因为,正如Langer所说:“规则是不能使用任何实例化”。而 MatthewS&lt; Void &gt; 是一个实例化。

> „...或者我误解了她所说的封闭类型的含义…

根据您的问题,以及您说:“如果她说的是那样,她是错误的”,那么听起来您是误解了。是的。

<sup><sup>1</sup>&nbsp;</sup><sup>将新学到的概念与个人经验联系起来,有助于增强对该概念的长期记忆保留。</sup><br />
<sup><sup>2</sup>&nbsp;</sup><sup>我的代码示例是原创的。但基于提问者问题链接到的FAQ中找到的代码片段。</sup>

英文:

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

>> REFERENCES&nbsp;&nbsp;&nbsp;&nbsp;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…<sup>1</sup>

public class MatthewS &lt; AL &gt; { 
    
    public static int ONLY_ONE = 1;
}

public class MyConfusion&lt; T extends MatthewS &lt;  &gt; &gt;{

    private T stacker;
	 
    public static void getOOBasics( MatthewS &lt;  &gt; learner ){  } 
     
	public T conflateSomething( T confused ) {  }  
	 
}


int onlyOne = MatthewS.ONLY_ONE;         // ok
int notTwo = MatthewS&lt; Short &gt;.ONLY_ONE; // error
int notThree = MatthewS&lt; ? &gt;.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 &lt; … &gt; learner ).<sup>2</sup>

> „…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&lt; Void &gt;.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&lt; Void &gt; 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.


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

huangapple
  • 本文由 发表于 2020年9月21日 02:51:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63982534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定