Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.654 :

Posted: Wed Mar 21, 2018 4:48 am
by TwistedLizard
Option 1:
Non-static inner class cannot have static members.

Isn't listed as a correct response. Is that an error?

Code: Select all

class Test{
  class X{
    //static int i = 1;        //error
    final static int j = 2;    //ok
  }
}

Re: About Question com.enthuware.ets.scjp.v6.2.654 :

Posted: Wed Mar 21, 2018 7:56 am
by admin
Because, as your example shows, and as we discussed in viewtopic.php?f=2&t=4519, they can be as long as they are constant variable.

Re: About Question com.enthuware.ets.scjp.v6.2.654 :

Posted: Wed Mar 21, 2018 9:33 am
by TwistedLizard
admin wrote:Because, as your example shows, and as we discussed in viewtopic.php?f=2&t=4519, they can be as long as they are constant variable.
duh! Sorry. My brain must have been switched off when I posted that:)
Can I delete the thread :oops:

Re: About Question com.enthuware.ets.scjp.v6.2.654 :

Posted: Tue Jan 25, 2022 1:39 pm
by keanu777
In Java 16+ it is possible

Re: About Question com.enthuware.ets.scjp.v6.2.654 :

Posted: Sat Oct 28, 2023 10:15 am
by andrew1584
Why is this marked as a correct answer?
"Anonymous classes cannot be static."

Difference between static and non-static classes - non-static classes initiated in context of outer class instance and always have a reference to this instance. Anonymous class can be initiated in a static block - in this case it wont have any reference to outer class instance:

Code: Select all

public class App {
    static Runnable staticRef;

    static {
        staticRef = new Runnable() {
            @Override
            public void run() {
//                System.out.println(App.this); Wont compile
            }
        };

    }
}
Anonymous class doesn't have an explicit constructor, but you can use reflection to create an instance of class without Outer class instance:

Code: Select all

        var clazz = App.staticRef.getClass();
        var newInst = clazz.getDeclaredConstructors()[0].newInstance();
So it behaves as any other static inner class.

I found that before JLS was explicitly saying:
An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).

https://docs.oracle.com/javase/specs/jl ... jls-15.9.5

However, JLC SE17 doesn't have this statement anymore:
https://docs.oracle.com/javase/specs/jl ... jls-15.9.5

Re: About Question com.enthuware.ets.scjp.v6.2.654 :

Posted: Sat Oct 28, 2023 8:59 pm
by admin
Actually, Section 15.9.5 of JLS 17 says, "An anonymous class is always an inner class (§8.1.3)" and in section 8.1.3, it says, "An inner class is a nested class that is not explicitly or implicitly static." Further down, it says that anonymous class is an inner class.

So, together, the statements imply that anonymous classes are not static. They may behave as static classes, but they are not.

The same statements exist in JLS 21, so I suspect there is a genuine reason they are hell bent on categorizing anonymous classes as non-static, but that reason is not clear.

Re: About Question com.enthuware.ets.scjp.v6.2.654 :

Posted: Wed Nov 01, 2023 7:39 am
by andrew1584
Hm, interesting, ty.
I guess they just try to avoid unnecessary classification, because it will lead to unnecessary limitations. Static, member, local - for a typical anonymous class use-case it just doesn't matter. Kind of a grey area.

Re: About Question com.enthuware.ets.scjp.v6.2.654 :

Posted: Tue Nov 07, 2023 12:01 am
by servebeneath
andrew1584 wrote:
Sat Oct 28, 2023 10:15 am
Why is this marked as a correct answer?
"Anonymous classes cannot be static."

Difference between static and non-static classes - non-static classes initiated in context of outer class instance and always have a reference to this instance. Anonymous class can be initiated in a static block - in this case it wont have any reference to outer class instance:

Code: Select all

public class App {
    static Runnable staticRef;

    static {
        staticRef = new Runnable() {
            @Override
            public void run() {
//                System.out.println(App.this); Wont compile
            }
        };

    }
}
Anonymous class doesn't have an explicit constructor, but you can use reflection to create an instance of class without Outer class instance:

Code: Select all

        var clazz = App.staticRef.getClass();
        var newInst = clazz.getDeclaredConstructors()[0].newInstance();
So it behaves as any other static inner class.

I found that before JLS was explicitly saying:
An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).

https://docs.oracle.com/javase/specs/jl ... jls-15.9.5

However, JLC SE17 doesn't have this statement anymore:
https://docs.oracle.com/javase/specs/jl ... jls-15.9.5
Good to know. :)