About Question enthuware.ocpjp.v7.2.1464 :
Moderators: Site Manager, fjwalraven
About Question enthuware.ocpjp.v7.2.1464 :
The explanation of correct answer is: "An enum's constructor is private by default. You cannot apply public or protected to the constructor. private is allowed but is redundant."
But The Java Tutorials says: "The constructor for an enum type must be package-private or private access".
So what is correct?
But The Java Tutorials says: "The constructor for an enum type must be package-private or private access".
So what is correct?
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1464 :
As per section 8.9.2 of JLS:
HTH,
Paul.
You can verify this by defining an enum and run javap on it. You will see that the output will not show you any constructors even if you don't specify any access modifier for the constructor. That means it is private.It is a compile-time error if a constructor declaration of an enum type is public or protected.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Tue Dec 10, 2013 9:10 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1464 :
One of the blue answers explains "An enum definition can only be either public or default (i.e. no access modifier)" but the detailed explanation below states "An enum is implicitly declared public."
If enums are implicitly public, how do I write one that is default scope?
Thanks!
Krishna
If enums are implicitly public, how do I write one that is default scope?
Thanks!
Krishna
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1464 :
There is no scope by the name "default". Default means whatever happens when there is no modifier applied.
In case of enums, as the explanation says, you can only put public modifier. If you don't put any modifier i.e. the default situation, the enum will still be public. In other words, the default scope is public for enums.
A similar situation exists with methods declared in an interface. They are implicitly public. So even if you do not put any modifier, they are still public. In other words, the default scope for methods of an interface is public.
In case of classes, the default scope is a little more complicated. There is no one word to describe it so people just call it the "default" scope. But the fact is "default" is different for different things.
HTH,
Paul.
In case of enums, as the explanation says, you can only put public modifier. If you don't put any modifier i.e. the default situation, the enum will still be public. In other words, the default scope is public for enums.
A similar situation exists with methods declared in an interface. They are implicitly public. So even if you do not put any modifier, they are still public. In other words, the default scope for methods of an interface is public.
In case of classes, the default scope is a little more complicated. There is no one word to describe it so people just call it the "default" scope. But the fact is "default" is different for different things.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Tue Dec 10, 2013 9:10 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1464 :
Ah, I incorrectly took default in "An enum definition can only be either public or default (i.e. no access modifier)" to mean package visibility; "default" as you say is different depending on context.
Thanks for the reply!
Thanks for the reply!
-
- Posts: 5
- Joined: Sat Aug 12, 2017 1:43 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1464 :
But when we create enum as inner class - we can use private and protected modifier
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1464 :
Yes, the restrictions on access modifiers are for top level reference types only. Nested reference types can have any modifier. For example, even a class or an interface can be private or protected.
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Tue Sep 09, 2014 3:28 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1464 :
exactly, therefore the answer "An enum definition can only be either public or default (i.e. no access modifier)." is not correct
thats correct, but, if you try to extend that protected enum and you will get a compilation error "Cannot inherit from final '[classname]'"
so, although one can create a protected enum, its kind of silly since one can never call/use its constants or methods.
private enums are perfectly sound since, as long as they are not a top-level class enum, they can be used inside the outer class
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1464 :
Explanation has been updated to make it clear that it is talking about top level enum.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 7 guests