About Question enthuware.ocpjp.v8.2.1437 :

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

Moderator: admin

Post Reply
schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

About Question enthuware.ocpjp.v8.2.1437 :

Post 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

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

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

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

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post 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

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

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

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

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post 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

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post 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

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

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

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

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post 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

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

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

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

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post 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

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

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

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

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post by schchen2000 »

Thank you. I learn something new everyday.

Schmichael

frodank
Posts: 1
Joined: Sat Mar 19, 2016 11:21 am
Contact:

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

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

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

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

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

nikitos
Posts: 21
Joined: Mon Oct 24, 2016 6:55 am
Contact:

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

Post 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:)

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

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

Post 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.
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 35 guests