2

The JLS 15.9.2 tells us how to determine an enclosing instance: Let C be the class being instantiated, and let i be the instance being created.

If C is an inner class, then i may have an immediately enclosing instance (§8.1.3), determined as follows:

[...]

If C is a local class, then:

  • If C occurs in a static context, then i has no immediately enclosing instance.

  • Otherwise, if the class instance creation expression occurs in a static context, then a compile-time error occurs.

  • Otherwise, let O be the immediately enclosing class of C. Let n be an integer such that O is the n'th lexically enclosing type declaration of the class in which the class instance creation expression appears.

The immediately enclosing instance of i is the n'th lexically enclosing instance of this.

I didn't get what the bolded case means. Let me provide the example I wasn't supposed to be compiled:

class A{
    int a;
    public static void main (String[] args) throws java.lang.Exception{
        class Foo{
            void bar(){
            }
        }
        Foo f = new Foo(); //Instance creation expression occured in the static context
    }
}

DEMO

What's wrong with that? Couldn't you provide an actual example describing the second point?

2 Answers 2

0

You should read these two lines :

  • If C occurs in a static context, then i has no immediately enclosing instance.

  • Otherwise, if the class instance creation expression occurs in a static context, then a compile-time error occurs.

Your case is the first case - you defined the class Foo in a static context (the main method), and therefore the instance f has no enclosing instance.

If, however, you'd define the class Foo outside the main method, and try to create the instance of Foo in the main method, you'll get an error, unless you change Foo to be a static class.

class A
{
    int a;
    class Foo
    {
        void bar()
        {
        }
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        Foo f = new Foo(); // this should fail
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

But Foo is not a local class. The quote is talking about local classes.
Moreover there was no anything about direct inner classes of some another class which are defined in docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1
0

I guess the following case is meant:

public class A {

   public class B { /* ... */ }

   public static void createB() {
      new B();  // <<=== This should fail
   }
}

The marked line should fail, since the inner class B is not static, so it requires en enclosing instance of A. There is no such enclosing instance, since the method createB() is static.

UPDATE: I mistook the question beeing about inner classes in general, which is what my example shows. In the context of Local Classes I also can't interpret the documentation.

3 Comments

But the B class is not a local class. I thought the marked line was talking about exactly local classes.
OK, I give up. I also don't know what that sentence means. I'll have to dig in further.
The thing is when the declaration of the local class occured in a non-static context, how is it even possible to embed the instance creation expression of the class into a static-context. Is it ever possible?

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.