Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why instantiation of static nested class object is allowed?

Tags:

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() 
like image 870
Parag Meshram Avatar asked Oct 11 '12 09:10

Parag Meshram


People also ask

Can static nested class be instantiated?

A static nested class may be instantiated without instantiating its outer class. Inner classes can access both static and non-static members of the outer class. A static class can access only the static members of the outer class.

Would you use to create an object for the static nested class?

For example, to create an object for the static nested class, use this syntax: OuterClass. StaticNestedClass nestedObject = new OuterClass. StaticNestedClass();

Can you instantiate static classes?

A static inner class can be instantiated without the need for an instance of the outer class. In general, an Inner class is a part of nested class, called Non-static nested classes in Java. The types of inner classes are member inner class, anonymous inner class, and local inner class.

What is the purpose of static nested class?

Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience. Save this answer.


2 Answers

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?

like image 130
Jon Skeet Avatar answered Oct 11 '22 23:10

Jon Skeet


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

You may need to use a nested class in a static context, for example:

public class Test {      public static void main(String args[]) {         InnerClass innerClass = new InnerClass();     }      class InnerClass {      } }   

In this case, when you try to instantiate the innerClass you get the error:

No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).

To avoid this, you could instantiate an object of type Test and create an instance of innerClass from it:

Test test = new Test(); InnerClass innerClass = test.new InnerClass(); 

or better, declare also the innerClass as static and instantiate it in a static context:

public class Test {      public static void main(String args[]) {         InnerClass innerClass = new InnerClass();     }      static class InnerClass {      } } 
like image 23
Alessandro Avatar answered Oct 11 '22 21:10

Alessandro