About Question enthuware.ocpjp.v8.2.1437 :
Moderator: admin
-
- Posts: 106
- Joined: Mon Mar 28, 2016 11:36 pm
- Contact:
About Question enthuware.ocpjp.v8.2.1437 :
class TestClass{
void someMethod(){
Title.DR dr = new Title.DR("Dr. ");
}
}
This is one of the answer options and the explanation below says "You cannot create new enum constants outside the enum definitions."
Does that mean that you can create new enum constants INSIDE the enum definitions?
I tried it and it failed to work.
Do you mind if you can use that block of code in an example? Many thanks.
Schmichael
void someMethod(){
Title.DR dr = new Title.DR("Dr. ");
}
}
This is one of the answer options and the explanation below says "You cannot create new enum constants outside the enum definitions."
Does that mean that you can create new enum constants INSIDE the enum definitions?
I tried it and it failed to work.
Do you mind if you can use that block of code in an example? Many thanks.
Schmichael
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
enum constants cannot be instantiated/created with the new keyword, but you can create a new constant inside that enum by simply defining a new one. For example:
enum Title
{
MR("Mr. "), MRS("Mrs. "), MS("Ms. "), DR("Dr. ");
}
The statement is talking about creating of new enum constants (not their instantiation). But I can see that the statement in the explanation is not very clear.
Updated.
thank you!
Paul.
enum Title
{
MR("Mr. "), MRS("Mrs. "), MS("Ms. "), DR("Dr. ");
}
The statement is talking about creating of new enum constants (not their instantiation). But I can see that the statement in the explanation is not very clear.
Updated.
thank you!
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 106
- Joined: Mon Mar 28, 2016 11:36 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
Could you please clarify what you meant when you saidadmin wrote:enum constants cannot be instantiated/created with the new keyword, but you can create a new constant inside that enum by simply defining a new one. For example:
enum Title
{
MR("Mr. "), MRS("Mrs. "), MS("Ms. "), DR("Dr. ");
}
The statement is talking about creating of new enum constants (not their instantiation). But I can see that the statement in the explanation is not very clear.
Updated.
thank you!
Paul.
"The statement is talking about creating of new enum constants (not their instantiation). But I can see that the statement in the explanation is not very clear."
Thanks.
Let me get this straight.
What the following attempted to do was to add (to the existing Title enum) a new enum constant possibly with a name of DR("Dr. ") but it's all done THE WRONG WAY by using the keyword new with it. In other words, the keyword new and enum are sworn enemies. They never go together. Is that correct?
class TestClass{
void someMethod(){
Title.DR dr = new Title.DR("Dr. ");
}
}
The right way should have been by adding DR("Dr. ") directly to Title enum. Is that what you were trying to explain in your earlier answer? Thanks.
Schmichael
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
Yes, that is correct. Enums cannot be instantiated. But of course, the JVM does create instances of the enum for each one defined in that enum. If you want more constants in that enum, you have to add them in the enum definition.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 106
- Joined: Mon Mar 28, 2016 11:36 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
Many thanks for your time, Paul. It's greatly appreciate it.admin wrote:Yes, that is correct. Enums cannot be instantiated. But of course, the JVM does create instances of the enum for each one defined in that enum. If you want more constants in that enum, you have to add them in the enum definition.
HTH,
Paul.
Schmichael
-
- Posts: 106
- Joined: Mon Mar 28, 2016 11:36 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
Paul,admin wrote:Yes, that is correct. Enums cannot be instantiated. But of course, the JVM does create instances of the enum for each one defined in that enum. If you want more constants in that enum, you have to add them in the enum definition.
HTH,
Paul.
When you said "But of course, the JVM does create instances of the enum for each one defined in that enum,"
you were referring to having a constructor inside an enum definition. Is that correct? Thanks.
Schmichael
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
Yes, that is correct. Even if you don't explicitly define a constructor for an enum, one will be provided by the compile (just like it does for a regular class) and it will be invoked for each value.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 106
- Joined: Mon Mar 28, 2016 11:36 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
The constructor provided by the compiler would only work withadmin wrote:Yes, that is correct. Even if you don't explicitly define a constructor for an enum, one will be provided by the compile (just like it does for a regular class) and it will be invoked for each value.
-Paul.
enum Titles{
MR, MRS();
// These 2 enum constants will work with the compiler-provided constructor.
// Something like MR("Mr.") will not work with the compiler-provided constructor
}
Thanks a lot, Paul. Much appreciated.
Schmichael
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
Yes, as I mentioned before the default constructor for an enum is just like the default constructor provided for a regular class. One exception that it is always private.
Also, why would you expect a constructor (or a method) that doesn't take any parameter to work for a call that takes an argument? If you are passing an argument in the constructor (like we are doing in this particular enum), then you have to define that constructor explicitly (again, just like you would do in a regular class.).
Also, why would you expect a constructor (or a method) that doesn't take any parameter to work for a call that takes an argument? If you are passing an argument in the constructor (like we are doing in this particular enum), then you have to define that constructor explicitly (again, just like you would do in a regular class.).
If you like our products and services, please help us by posting your review here.
-
- Posts: 106
- Joined: Mon Mar 28, 2016 11:36 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
Not always private. A constructor inside an enum CAN ALSO take default access specifier, i.e. leaving out any access specifier immediately before the constructor inside an enum is the same as the constructor having the default access specifier.One exception that it (the constructor) is always private.
You raised a very good question. Fair enough. Thanks a lot for your time.why would you expect a constructor (or a method) that doesn't take any parameter to work for a call that takes an argument?
Schmichael
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
No, even if you leave out the access specifier (i.e. default), it will still be private. You may want to check out https://docs.oracle.com/javase/specs/jl ... #jls-8.9.2 :schchen2000 wrote:Not always private. A constructor inside an enum CAN ALSO take default access specifier, i.e. leaving out any access specifier immediately before the constructor inside an enum is the same as the constructor having the default access specifier.One exception that it (the constructor) is always private.
Schmichael
In an enum declaration, a constructor declaration with no access modifiers is private.
In an enum declaration with no constructor declarations, a default constructor is implicitly declared. The default constructor is private, has no formal parameters, and has no throws clause.
If you like our products and services, please help us by posting your review here.
-
- Posts: 106
- Joined: Mon Mar 28, 2016 11:36 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
Thank you. I learn something new everyday.
Schmichael
Schmichael
-
- Posts: 1
- Joined: Sat Mar 19, 2016 11:21 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
Isn't this one correct?
System.out.println(MR.format("Rob", "Miller"));
You don't have to write "Title." before the enum-value if you do a static import on MR.
System.out.println(MR.format("Rob", "Miller"));
You don't have to write "Title." before the enum-value if you do a static import on MR.
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
It is possible but not in this case. Both, Title and TestClass are being defined in the same file. There is no package statement. So how will you import Title statically?frodank wrote:Isn't this one correct?
System.out.println(MR.format("Rob", "Miller"));
You don't have to write "Title." before the enum-value if you do a static import on MR.
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Mon Oct 24, 2016 6:55 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
I do not agree with this:
class TestClass{ void someMethod() { System.out.println(MR.format("Rob", "Miller")); } }
If enum is defined in not default package - above code will works if we add next import:
import static a.Coffe.LATTE;
But if enum is defined in default package - it will not work:)
class TestClass{ void someMethod() { System.out.println(MR.format("Rob", "Miller")); } }
If enum is defined in not default package - above code will works if we add next import:
import static a.Coffe.LATTE;
But if enum is defined in default package - it will not work:)
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1437 :
The problem statement says that Title is accessible where required. So that means Title is imported. Therefore, you have to use Title.MR instead of simply using MR.
HTH,
Paul.
HTH,
Paul.
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 36 guests