About Question enthuware.ocpjp.v7.2.1437 :
Moderator: admin
-
- Posts: 28
- Joined: Wed Dec 04, 2013 7:57 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1437 :
Hi Paul,
This is a very good question. I learned a few things with it.
However I believe #3 should also be a good answer.
Thanks,
Tony
This is a very good question. I learned a few things with it.
However I believe #3 should also be a good answer.
Thanks,
Tony
-
- Site Admin
- Posts: 9941
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
Option 3 is not syntactically correct. The explanation explains why it is not correct.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Wed Dec 04, 2013 7:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
I tested it and it didn't give me any problem:
Thanks,
Tony
Code: Select all
enum Title {
MR("Mr. "), MRS("Mrs. "), MS("Ms. ");
private String title;
private Title(String s){
title = s;}
public String format(String first, String last){
return title+" "+first+" "+last;
}
class TestClass1{
void someMethod()
{
System.out.println(MR.format("Rob", "Miller"));}
}
public static void main(String[] args){
TestClass1 tc1 = Title.MR. new TestClass1();
tc1.someMethod();
}
}
Tony
Last edited by admin on Fri Mar 21, 2014 2:32 am, edited 1 time in total.
Reason: Applied code tags
Reason: Applied code tags
-
- Site Admin
- Posts: 9941
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
That is because your code has TestClass1 as the inner class of the enum. The code in question doesn't have any inner class.
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Tue Nov 04, 2014 1:13 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
What part in the Explanation explains why "System.out.println(MR.format("Rob", "Miller"));" is not valid?
-
- Site Admin
- Posts: 9941
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
Title is missing. It says right below the option,"It must be Title.MR.format(...)."
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 39
- Joined: Thu Jan 29, 2015 4:49 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
One more important fact is that in switch statement it is used the short version :
Title a = ..;
switch(a) {
case MR : ... ; // OK
case Title.MR : ...; // Compile error
}
Title a = ..;
switch(a) {
case MR : ... ; // OK
case Title.MR : ...; // Compile error
}
-
- Posts: 8
- Joined: Sun Dec 20, 2015 6:28 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
When I use the import statement:
This option (#3) seems to become valid:
And in the question it says:
Code: Select all
import static com.some.place.Title.*;
Code: Select all
void someMethod(){System.out.println(MR.format("Rob", "Miller"));}
So it sounds to me that one can assume this import.(Assume that Title is accessible wherever required.)
-
- Site Admin
- Posts: 9941
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
Title is accessible, not Mr.
If you like our products and services, please help us by posting your review here.
-
- Posts: 31
- Joined: Tue Oct 06, 2015 1:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
Hi Paul - Can you please explain why it is that in a switch statement you cannot use the name of the Enum (Title) but when calling enum methods you have to use both the Enum name and the enum constant type (Title.MR)? What is the reasoning behind this?
-
- Site Admin
- Posts: 9941
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
I am afraid the exact reason can only be given by the authors of the Java language but I couldn't find the it mentioned in JLS.krohani wrote:Hi Paul - Can you please explain why it is that in a switch statement you cannot use the name of the Enum (Title) but when calling enum methods you have to use both the Enum name and the enum constant type (Title.MR)? What is the reasoning behind this?
I can only guess that it is because of some lexical parsing issue. But I could be wrong.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 31
- Joined: Tue Oct 06, 2015 1:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
No worries, I asked because 1) i was just curious and 2) I figured the answer would help me remember these points better 

-
- Posts: 22
- Joined: Thu Dec 09, 2021 11:23 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
Hello, question. Can you please give an example on how to use Title2 and Title for below? Thanks.
Code: Select all
enum Title2 {
DR;
private Title t = Title.MR;
public String format(String s){ return t.format(s, s); };
}
-
- Site Admin
- Posts: 9941
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
Not sure what you mean by how to use. You mean how to refer to them in some other class?
Post some code to show what exactly are you trying to get at.
Post some code to show what exactly are you trying to get at.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Thu Dec 09, 2021 11:23 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
Is this like a nested enum? am not sure how to use this. Thanks.
-
- Site Admin
- Posts: 9941
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1437 :
You can use the Title2 enum just like you use any other enum. Like this:
Code: Select all
void m(){
Title2 t2 = Title2.DR;
Title t1 = Title.MR;
}
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 4 guests