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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

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

Post 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
  }
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

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

Post 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:

keanu777
Posts: 1
Joined: Sun Feb 28, 2021 5:00 am
Contact:

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

Post by keanu777 »

In Java 16+ it is possible

andrew1584
Posts: 8
Joined: Sat Oct 28, 2023 9:27 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

andrew1584
Posts: 8
Joined: Sat Oct 28, 2023 9:27 am
Contact:

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

Post 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.

servebeneath
Posts: 1
Joined: Mon Nov 06, 2023 11:58 pm
Contact:

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

Post 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. :)

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 41 guests