About Question enthuware.ocpjp.v7.2.1464 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
Ryhor

About Question enthuware.ocpjp.v7.2.1464 :

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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

krimb1
Posts: 2
Joined: Tue Dec 10, 2013 9:10 pm
Contact:

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

Post 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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

krimb1
Posts: 2
Joined: Tue Dec 10, 2013 9:10 pm
Contact:

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

Post 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!

kakawi
Posts: 5
Joined: Sat Aug 12, 2017 1:43 pm
Contact:

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

Post by kakawi »

But when we create enum as inner class - we can use private and protected modifier

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

jbilkes
Posts: 21
Joined: Tue Sep 09, 2014 3:28 pm
Contact:

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

Post 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

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

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

Post by admin »

Explanation has been updated to make it clear that it is talking about top level enum.

thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests