Page 1 of 1

About Question enthuware.ocpjp.v8.2.1782 :

Posted: Sun Apr 11, 2021 11:21 am
by jme_chg
I tested the code but changed the enum so that it was just:

Code: Select all

enum Genre { DRAMA, THRILLER, HORROR, ACTION }; //***i.e. not static
but I thought this would cause:

Code: Select all

new Movie("Titanic", Movie.Genre.DRAMA, 'U'),                 
new Movie("Psycho", Movie.Genre.THRILLER, 'U'),                 
new Movie("Oldboy", Movie.Genre.THRILLER, 'R'),                 
new Movie("Shining", Movie.Genre.HORROR, 'U')); 
to fail compilation since
static members: can use enclosing class' name
non-static members: must use instance of enclosing class

but it still worked.

And also if have:

Code: Select all

new Movie("Titanic", new Movie().Genre.DRAMA, 'U'),                 
it fails compilation regardless of whether the enum is static/non-static.

Is there an explanation for this? :/

Re: About Question enthuware.ocpjp.v8.2.1782 :

Posted: Sun Apr 11, 2021 10:53 pm
by admin
As per section 8.9 of JLS 11 specification:
A nested enum type is implicitly static. It is permitted for the declaration of a nested enum type to redundantly specify the static modifier.