About Question enthuware.ocpjp.v7.2.1457 :
Moderator: admin
-
- Posts: 1
- Joined: Tue Aug 12, 2014 8:53 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1457 :
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.
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.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1457 :
No, it is correct usage. It is same as, (in this case, DOG, CAT, and FISH).
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Sun Feb 21, 2016 10:12 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1457 :
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?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1457 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Wed Oct 19, 2016 12:16 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1457 :
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."
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1457 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 56
- Joined: Tue Feb 21, 2017 4:24 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1457 :
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)?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1457 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 37
- Joined: Mon Jun 12, 2017 2:16 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1457 :
I read, that enum's Konstructor always must be declared private, isn't that true ?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1457 :
It is implicitly private. So even if you don't declare it explicitly as private, the compiler will make it private.
If you like our products and services, please help us by posting your review here.
-
- Posts: 37
- Joined: Mon Jun 12, 2017 2:16 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1457 :
Thank you !
Who is online
Users browsing this forum: Bing [Bot] and 116 guests