Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

java - Why instantiation of static nested class object is allowed?

I have started learning Java language for Android Application developement.

As per my understanding based on static class, we cannot instantiate object of static class.

But why instantiation of static nested class object is allowed in following situaltion?

class EnclosingClass 
{     
      //...     
      class static StaticInnerClass 
      {         
          //...     
      } 
} 

Why we can create object of inner class if it is marked as static?

EnclosingClass.StaticInnerClass s = new EnclosingClass.StaticInnerClass()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As per my understanding based on static class, we cannot instantiate object of static class.

Your understanding of the meaning of "static class" is incorrect. Basically a "static class" in Java is a nested class which doesn't have an implicit reference to an instance of the containing class. See section 8.5.1 of the JLS for more information, in particular:

The static keyword may modify the declaration of a member type C within the body of a non-inner class or interface T. Its effect is to declare that C is not an inner class. Just as a static method of T has no current instance of T in its body, C also has no current instance of T, nor does it have any lexically enclosing instances.

Perhaps you were thinking of static classes in C#, which are completely different?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...