Why instantiation of static nested class object is allowed?
Asked Answered
G

3

24

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()
Gilbertgilberta answered 11/10, 2012 at 9:34 Comment(3)
Here's a good quote from the docs: "Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes." (Link). And if you scroll down they explain their usage.Adna
@ParagMeshram What do you expect from your bounty? Is Jon Skeet's answer not clear?Mechanical
Another clear and helpful quote from the docs (docs.oracle.com/javase/tutorial/java/javaOO/nested.html) is this: "In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience."Debouch
A
31

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?

Aloft answered 11/10, 2012 at 9:37 Comment(10)
Don't you mean a static inner class?Adna
@Keyser: No, I mean a nested class. See the quote from the JLS: a static class indicates that the class is not an inner class. From section 8.1.3 of the JLS: "An inner class is a nested class that is not explicitly or implicitly declared static."Aloft
I just got it, thanks (the quote I posted below the question helped) :)Adna
"Its effect is to declare that C is not an inner class" I don't see why they didn't just make a new keyword, declaring something "static" to mean "non-inner" is very misleading don't ya think? Are there other implications of declaring an nested class as static?Gamic
@SamusArin: Well it's sort of the same as the normal meaning of static in terms of "related to a type rather than any instance of a type" - an inner class has a reference to an instance of its containing class (just like an instance method does); a static nested class doesn't. There are various restrictions about what's allowed within each kind of nested type; I wouldn't claim to be an expert on them. As for introducing a new keyword - a new full keyword would have been a breaking change. (What if someone already had a variable with that name?)Aloft
@JonSkeet I figured an additional keyword would be "too big" of a deal, however its just weird to me that you can instantiate a static class (doe's java allow "normal" / non-nested static classes to be instantiated with the new() operator/kewyord)? Sorry to bother you with such trivialities, but I really appreciate it, thanks.Gamic
@SamusArin: There's no such thing as a "non-nested static class".Aloft
@JonSkeet OK, well I've been programming in C# these day's, so that's my problem, b/c it does have them. Thanks a lot!Gamic
@SamusArin: Right - C#'s static classes and Java's nested static classes are completely different. Java doesn't have anything like C#'s static classes, and C# doesn't have anything like Java's inner classes. (If you want a reference to an instance of the containing class in C#, you need to make it explicit.)Aloft
@JonSkeet Heh, I know all about adding the explicit reference now. I code in Monodroid, and I used to port so many java android snippet containing inner classes in a very "non-ideal" manner until I found a post here about imitating java's inner classes in c# using an explicit reference. I then had to go back and rewrite so much code.Gamic
F
3

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 {

    }
}
Flam answered 22/3, 2017 at 11:30 Comment(0)
C
0

check it, maybe it can help you Nested Classes

Cristacristabel answered 11/10, 2012 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.