Page 1 of 1

About Question enthuware.ocpjp.v7.2.1464 :

Posted: Fri Jan 04, 2013 3:32 pm
by Ryhor
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?

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

Posted: Fri Jan 04, 2013 4:46 pm
by admin
As per section 8.9.2 of JLS:
It is a compile-time error if a constructor declaration of an enum type is public or protected.
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.

HTH,
Paul.

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

Posted: Wed Apr 09, 2014 11:05 am
by krimb1
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

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

Posted: Wed Apr 09, 2014 11:17 am
by admin
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.

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

Posted: Wed Apr 09, 2014 12:25 pm
by krimb1
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!

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

Posted: Mon Aug 14, 2017 1:52 am
by kakawi
But when we create enum as inner class - we can use private and protected modifier

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

Posted: Mon Aug 14, 2017 9:28 pm
by admin
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.

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

Posted: Mon Oct 15, 2018 8:59 am
by jbilkes
admin wrote:
Mon Aug 14, 2017 9:28 pm
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.
exactly, therefore the answer "An enum definition can only be either public or default (i.e. no access modifier)." is not correct
kakawi wrote:
Mon Aug 14, 2017 1:52 am
But when we create enum as inner class - we can use private and protected modifier
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

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

Posted: Mon Oct 15, 2018 10:16 am
by admin
Explanation has been updated to make it clear that it is talking about top level enum.

thank you for your feedback!