Page 1 of 1

About Question enthuware.ocpjp.v7.2.1457 :

Posted: Tue Aug 12, 2014 8:57 pm
by pepness
There is a typo in the last declaration:

Enum constants (here, DOG, CAT, and FISH) must be declared before anything else. Therefore, String name; is invalid here.

Should be:

Enum constants here (DOG, CAT, and FISH) must be declared before anything else. Therefore, String name; is invalid here.

Re: About Question enthuware.ocpjp.v7.2.1457 :

Posted: Tue Aug 12, 2014 11:53 pm
by admin
No, it is correct usage. It is same as, (in this case, DOG, CAT, and FISH).

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1457 :

Posted: Sun Mar 20, 2016 5:09 am
by sumanenthu
I got from somewhere that Enum constants are initialized through the static initializer methods. Then how can we use 'this' keyword in the constructor in option 3?

Re: About Question enthuware.ocpjp.v7.2.1457 :

Posted: Sun Mar 20, 2016 9:46 pm
by admin
You can always use "this" keyword in any constructor.
It is really very difficult to comment on the correctness of a statement when taken out of context but the statement, " Enum constants are initialized through the static initializer methods." doesn't look correct to me.
-Paul.

Re: About Question enthuware.ocpjp.v7.2.1457 :

Posted: Wed Oct 19, 2016 12:18 pm
by alamizsna
As I know Enums are always final, so how come this ->Option 3: "Here, CAT is actually an instance of an anonymous subclass of Pets."

Re: About Question enthuware.ocpjp.v7.2.1457 :

Posted: Wed Oct 19, 2016 1:32 pm
by admin
They are not always final. If you create anonymous subclass as done in option 3, Pets will not be made final by the compiler. For CAT, the compiler will create a class named Pets$1, which will be final.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1457 :

Posted: Fri May 19, 2017 2:03 pm
by lenalena
For option 2, why isn't static String prefix considered effectively final? Or it is effectively final but enum can only access static fields that are explicitly finals? Or the concept of effectively final doesn't apply to static fields (because they could be modified outside of the declaring class)?

Re: About Question enthuware.ocpjp.v7.2.1457 :

Posted: Fri May 19, 2017 10:00 pm
by admin
The concept of effectively final applies only the method parameters and method local (aka automatic) variables. It does not apply to class or instance variables. The reason is simple. Method parameters and automatic variables are kept on the stack space and go out of scope as soon as the method is over. class and instance variables are stored on the heap space and never go out of scope (as long as the class/object is there) so there is no telling when their value might change if they are not declared explicitly final.

Re: About Question enthuware.ocpjp.v7.2.1457 :

Posted: Thu Oct 12, 2017 3:06 am
by horst1a
I read, that enum's Konstructor always must be declared private, isn't that true ?

Re: About Question enthuware.ocpjp.v7.2.1457 :

Posted: Thu Oct 12, 2017 3:09 am
by admin
It is implicitly private. So even if you don't declare it explicitly as private, the compiler will make it private.

Re: About Question enthuware.ocpjp.v7.2.1457 :

Posted: Thu Oct 12, 2017 3:49 am
by horst1a
Thank you !