About Question enthuware.ocpjp.ii.v11.2.1360 :

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

Moderator: admin

Post Reply
ChristianMatte
Posts: 1
Joined: Fri Nov 20, 2020 10:44 am
Contact:

About Question enthuware.ocpjp.ii.v11.2.1360 :

Post by ChristianMatte »

The answer is wrong: a local class cannot extend any class. The correct answer is "None of the above".
If a local class extends an inner class inside a static context, then a compilation error is due (a local class in a static context cannot reference non-statitc members of its enclosing class).
The following code will generate a compilation error at Line 0:

public class DummyClass {
class DummyInnerClass{}

static void staticMethod(){
class DummyLocalClass extends DummyInnerClass{} //Line 0
}
}

P.S.:The Java Language Specification states that "A local class is a nested class", and "All local classes are inner classes".
Reference: https://docs.oracle.com/javase/specs/jl ... l#jls-14.3

Furthermore, the specifications also states that "An inner class is a nested class that is not explicitly or implicitly declared static".
Reference: https://docs.oracle.com/javase/specs/jl ... #jls-8.1.3
Meaning that a local class fits the criteria as a "non-static nested class".

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

Re: About Question enthuware.ocpjp.ii.v11.2.1360 :

Post by admin »

Your example is not valid because you are trying to extend a non-static class inner class from a static context (which is not accessible because there is no "this"). This is not because of a restriction on inner classes but a general restriction on accessing any instance member from a static context without a reference to the object.

If you make DummyInnerClass static, it will work.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 6 guests