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.
About Question enthuware.ocpjp.v7.2.1460 :
Moderator: admin
-
- Posts: 5
- Joined: Thu Mar 06, 2014 9:29 am
- Contact:
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1460 :
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.)
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sat Apr 27, 2013 7:34 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1460 :
Good day.
Why does the answer is wrong? The code is compiling and running well.
Why does the answer is wrong? The code is compiling and running well.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1460 :
Unless mentioned otherwise, you need to fill all the yellow targets.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Thu Jan 14, 2016 8:34 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1460 :
At point 1 of the exaplanation for this question is said:
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
Is the first statement actually true?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.
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
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1460 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Thu Jan 14, 2016 8:34 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1460 :
Thank you very much. The point I was missing was exactly the last statement
This is a different behavior from "normal" classes constructors.
Point to remember.
http://docs.oracle.com/javase/specs/jls ... #jls-8.8.3If no access modifier is specified for the constructor of an enum type, the constructor is private.
This is a different behavior from "normal" classes constructors.
Point to remember.
Who is online
Users browsing this forum: Bing [Bot] and 99 guests