About Question enthuware.ocpjp.v7.2.1460 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
erbegu
Posts: 5
Joined: Thu Mar 06, 2014 9:29 am
Contact:

About Question enthuware.ocpjp.v7.2.1460 :

Post by erbegu »

Can anyone explain why it is fine to put
"public enum X{ X1,X2,X3}" in the first line and "enum Y{Y1,Y2,Y3}" in the second box and it is not fine to do the oposite?
Thank you.

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

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

Post by admin »

It is the reverse that you can't do because you can't have two public classes (or enums/interfaces) in the same file. (Except if they are inner classes/enums/interfaces.)

Dadd80
Posts: 1
Joined: Sat Apr 27, 2013 7:34 am
Contact:

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

Post by Dadd80 »

Good day.
Why does the answer is wrong? The code is compiling and running well.
Image

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

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

Post by admin »

Unless mentioned otherwise, you need to fill all the yellow targets.

MarcoGC
Posts: 3
Joined: Thu Jan 14, 2016 8:34 am
Contact:

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

Post by MarcoGC »

At point 1 of the exaplanation for this question is said:
1) Enum constructor is always private. You cannot make it public or protected. If an enum type has no constructor declarations, then a private constructor that takes no parameters is automatically provided.
Is the first statement actually true?
I mean, I'm reading some cert books and I saw some code snippets where enum constructor had default access modifier.
Agree though they cannot have public or protected.
Thanks for the clarification.
Marco

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

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

Post by admin »

Yes, the given statement is correct.
default is not an access modifier. When there is no access modifier, that means it is default access.
For example,
class SomeClass{
default int x; <- this is invalid because default is not a valid access modifier
int y; <- this is valid and since there is no access modifer for y, it has default access.
}

In case of enums, when there is no access modifier for the constructor, it is considered private, that means default is private.

MarcoGC
Posts: 3
Joined: Thu Jan 14, 2016 8:34 am
Contact:

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

Post by MarcoGC »

Thank you very much. The point I was missing was exactly the last statement
If no access modifier is specified for the constructor of an enum type, the constructor is private.
http://docs.oracle.com/javase/specs/jls ... #jls-8.8.3
This is a different behavior from "normal" classes constructors.
Point to remember.

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests