About Question enthuware.ocpjp.v7.2.1457 :

Moderator: admin

pepness
Posts: 1
Joined: Tue Aug 12, 2014 8:53 pm
Contact:

About Question enthuware.ocpjp.v7.2.1457 :

Post by pepness » Tue Aug 12, 2014 8:57 pm

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.

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

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

Post by admin » Tue Aug 12, 2014 11:53 pm

No, it is correct usage. It is same as, (in this case, DOG, CAT, and FISH).

HTH,
Paul.

sumanenthu
Posts: 23
Joined: Sun Feb 21, 2016 10:12 am
Contact:

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

Post by sumanenthu » Sun Mar 20, 2016 5:09 am

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?

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

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

Post by admin » Sun Mar 20, 2016 9:46 pm

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.

alamizsna
Posts: 1
Joined: Wed Oct 19, 2016 12:16 pm
Contact:

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

Post by alamizsna » Wed Oct 19, 2016 12:18 pm

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

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

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

Post by admin » Wed Oct 19, 2016 1:32 pm

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.

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

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

Post by lenalena » Fri May 19, 2017 2:03 pm

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)?

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

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

Post by admin » Fri May 19, 2017 10:00 pm

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.

horst1a
Posts: 39
Joined: Mon Jun 12, 2017 2:16 am
Contact:

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

Post by horst1a » Thu Oct 12, 2017 3:06 am

I read, that enum's Konstructor always must be declared private, isn't that true ?

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

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

Post by admin » Thu Oct 12, 2017 3:09 am

It is implicitly private. So even if you don't declare it explicitly as private, the compiler will make it private.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests