Page 1 of 1

About Question enthuware.ocpjp.v8.2.1437 :

Posted: Mon Mar 28, 2016 11:46 pm
by schchen2000
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

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Tue Mar 29, 2016 12:37 am
by admin
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.

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Tue Mar 29, 2016 12:02 pm
by schchen2000
admin 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.
Could you please clarify what you meant when you said

"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

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Tue Mar 29, 2016 9:10 pm
by admin
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.

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Wed Mar 30, 2016 6:22 pm
by schchen2000
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.
Many thanks for your time, Paul. It's greatly appreciate it.

Schmichael

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Thu Mar 31, 2016 3:59 pm
by schchen2000
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.
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

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Thu Mar 31, 2016 7:46 pm
by admin
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.

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Thu Mar 31, 2016 11:16 pm
by schchen2000
admin 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.
The constructor provided by the compiler would only work with

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

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Fri Apr 01, 2016 12:18 am
by admin
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.).

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Fri Apr 01, 2016 1:33 am
by schchen2000
One exception that it (the constructor) is always private.
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.
why would you expect a constructor (or a method) that doesn't take any parameter to work for a call that takes an argument?
You raised a very good question. Fair enough. Thanks a lot for your time.

Schmichael

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Fri Apr 01, 2016 1:51 am
by admin
schchen2000 wrote:
One exception that it (the constructor) is always private.
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.

Schmichael
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 :
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.

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Fri May 20, 2016 11:34 pm
by schchen2000
Thank you. I learn something new everyday.

Schmichael

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Sat Feb 18, 2017 4:44 pm
by frodank
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.

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Sat Feb 18, 2017 11:00 pm
by admin
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.
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?

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Thu Apr 20, 2017 4:34 am
by nikitos
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:)

Re: About Question enthuware.ocpjp.v8.2.1437 :

Posted: Thu Apr 20, 2017 11:00 pm
by admin
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.