0

I would love to understand why the following instantiation will not compile:

Superclass.Subclass myObject = new Superclass.Subclass();

The error message reads:

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

What is meant by enclosing instance? Why is this necessary?

It seems this message is stating that the syntax must be:

Superclass mySuperObj = new Superclass();
Superclass.Subclass mySubObj = mySuperObj.new Subclass();

BUT it fails to explain what is wrong with my method or why this alternative syntax must be used.

1
  • Where is the code that declares Superclass and Subclass? You should post a minimal reproducible example - in your case, you should focus on the C of Complete. Commented Aug 17, 2017 at 13:25

3 Answers 3

2

The new [enclosing class].[enclosed class](...) idiom is used to initialize static nested classes, that is nested classes that are declared as a static member of their enclosing class.

The [enclosing class instance].new [enclosed class](...) idiom is used to initialize inner classes, that is, nested classes that are declared as an instance member of their enclosing class.

Examples

With...

class A {
    static class B {}
    class C {}
}

You will use:

  • new A.B()
  • new A().new C(), or with a given instance of A called a,
  • a.new C()

Note

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Mena. Your example illustrates that differing idioms must be used for each scenario. But why MUST there be two different idioms? Why can we not use the first idiom for BOTH situations? That is not clear to me.
@IqbalHamid you're welcome. Syntax is an arbitrary construct. Although in this case it is coherent with other idioms differentiating static member from instance member access. Which in turn, you need in Java, since those are very different contexts.
0

in this case you dont talk from sub class but inner class instead....

so, in this case to create an instance of an inner class you need an instance of the outer class, so you can do:

public class Foo {

    public static void main(String[] args) {
        Foo myFooObject = new Foo();
        Foo.InnerClass myFooInnerClass = myFooObject.new InnerClass();
        System.out.println(myFooObject);
        System.out.println(myFooInnerClass);
    }

    class InnerClass {
        @Override
        public String toString() {
            return "Am inner class";
        }
    }
}

Comments

0

The syntax to create an Inner class object is

 InnerClass innerObj = new OuterClass().new InnerClass(); 

NOT the

Superclass.Subclass myObject = new Superclass.Subclass();

Because : An instance of InnerClass can exist only within an instance of OuterClass.

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

InnerClass innerObj = new OuterClass().new InnerClass(); 

Read Java Docs for more details

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.