About Question enthuware.ocpjp.v7.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
tn1408
Posts: 28
Joined: Wed Dec 04, 2013 7:57 pm
Contact:

About Question enthuware.ocpjp.v7.2.1437 :

Post 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

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

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

Post by admin »

Option 3 is not syntactically correct. The explanation explains why it is not correct.
-Paul.
If you like our products and services, please help us by posting your review here.

tn1408
Posts: 28
Joined: Wed Dec 04, 2013 7:57 pm
Contact:

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

Post 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
Last edited by admin on Fri Mar 21, 2014 2:32 am, edited 1 time in total.
Reason: Applied code tags

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

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

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

SepticInsect
Posts: 20
Joined: Tue Nov 04, 2014 1:13 am
Contact:

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

Post by SepticInsect »

What part in the Explanation explains why "System.out.println(MR.format("Rob", "Miller"));" is not valid?

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

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

Post by admin »

Title is missing. It says right below the option,"It must be Title.MR.format(...)."

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

romsky
Posts: 39
Joined: Thu Jan 29, 2015 4:49 pm
Contact:

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

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

PtFyEH
Posts: 8
Joined: Sun Dec 20, 2015 6:28 pm
Contact:

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

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

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

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

Post by admin »

Title is accessible, not Mr.
If you like our products and services, please help us by posting your review here.

krohani
Posts: 31
Joined: Tue Oct 06, 2015 1:57 pm
Contact:

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

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

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

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

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

krohani
Posts: 31
Joined: Tue Oct 06, 2015 1:57 pm
Contact:

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

Post 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

yuir12
Posts: 22
Joined: Thu Dec 09, 2021 11:23 pm
Contact:

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

Post 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); }; 
}

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

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

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

yuir12
Posts: 22
Joined: Thu Dec 09, 2021 11:23 pm
Contact:

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

Post by yuir12 »

Is this like a nested enum? am not sure how to use this. Thanks.

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

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

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

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 37 guests