Page 1 of 1

About Question enthuware.ocpjp.v7.2.1437 :

Posted: Thu Mar 20, 2014 1:58 pm
by tn1408
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

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

Posted: Thu Mar 20, 2014 8:25 pm
by admin
Option 3 is not syntactically correct. The explanation explains why it is not correct.
-Paul.

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

Posted: Fri Mar 21, 2014 1:49 am
by tn1408
I tested it and it didn't give me any problem:

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();
       } 
    }
Thanks,

Tony

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

Posted: Fri Mar 21, 2014 2:27 am
by admin
That is because your code has TestClass1 as the inner class of the enum. The code in question doesn't have any inner class.

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

Posted: Mon Dec 01, 2014 11:30 am
by SepticInsect
What part in the Explanation explains why "System.out.println(MR.format("Rob", "Miller"));" is not valid?

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

Posted: Mon Dec 01, 2014 8:50 pm
by admin
Title is missing. It says right below the option,"It must be Title.MR.format(...)."

HTH,
Paul.

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

Posted: Tue Jun 30, 2015 5:56 pm
by romsky
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
}

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

Posted: Sat Jan 02, 2016 2:55 pm
by PtFyEH
When I use the import statement:

Code: Select all

import static com.some.place.Title.*;
This option (#3) seems to become valid:

Code: Select all

void someMethod(){System.out.println(MR.format("Rob", "Miller"));}
And in the question it says:
(Assume that Title is accessible wherever required.)
So it sounds to me that one can assume this import.

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

Posted: Sat Jan 02, 2016 9:17 pm
by admin
Title is accessible, not Mr.

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

Posted: Sat Jan 30, 2016 8:58 pm
by krohani
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?

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

Posted: Sat Jan 30, 2016 9:18 pm
by admin
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 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.
I can only guess that it is because of some lexical parsing issue. But I could be wrong.
HTH,
Paul.

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

Posted: Sat Jan 30, 2016 9:21 pm
by krohani
No worries, I asked because 1) i was just curious and 2) I figured the answer would help me remember these points better :D

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

Posted: Tue Jan 24, 2023 11:46 pm
by yuir12
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); }; 
}

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

Posted: Wed Jan 25, 2023 6:52 am
by admin
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.

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

Posted: Thu Jan 26, 2023 4:19 am
by yuir12
Is this like a nested enum? am not sure how to use this. Thanks.

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

Posted: Thu Jan 26, 2023 5:31 am
by admin
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;
}