About Question enthuware.ocpjp.v7.2.1418 :

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

Moderator: admin

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

About Question enthuware.ocpjp.v7.2.1418 :

Post by SepticInsect »

How do I know when to use Switch.OFF or just OFF?

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

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

Post by admin »

You just have to remember that within a switch you have to use OFF. Everywhere else you have to use Switch.OFF.

HTH,
Paul.
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.1418 :

Post by SepticInsect »

Ok. Thanks!

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

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

Post by PtFyEH »

In the expanaition it says:
... A reference of type I can be cast to any class at compile time. ...
But wouldn't it be better to say:
... A reference of type I can be cast to any non-final class at compile time. ...

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

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

Post by admin »

Not necessarily. What if a final class or any of its super classes implements the interface I?
But I see what you are getting at. The problem is if you try to make such statements too precise, they will boil down to the Java langauge specification. So we try to explain the general principle and then cover the finer points in different question separately if required.

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

Sergiy Romankov
Posts: 31
Joined: Thu Feb 19, 2015 8:25 am
Contact:

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

Post by Sergiy Romankov »

I believe, that enum types like a String types should be compared by using equals, why it is possible to use both options, and what is the defference?

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

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

Post by admin »

Enums are objects and every object in Java ultimately extends from Object class. Since Object class has a public equals method, you can use equals method on an enum as well.

The JVM ensures that there is exactly one instance for each member of the enumeration so it is OK (and, in fact, even recommended) to compare the enums using ==. That is why enums are allowed in switch statements as well. Object class's equals method also compares the references using == internally. So there is no difference unless, of course, the enum overrides the equals method, in which case, the result depends on how the equals method is implemented in the enum class.

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

Sergiy Romankov
Posts: 31
Joined: Thu Feb 19, 2015 8:25 am
Contact:

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

Post by Sergiy Romankov »

Thanks, very clear explanation.

alfredo.zuloaga
Posts: 10
Joined: Fri Nov 18, 2016 5:48 pm
Contact:

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

Post by alfredo.zuloaga »

I tryed with

public class TestClass {
enum Switch {
ON,
OFF
}
public static void main(String[] args) {
Switch s = Switch.OFF;
if (s == Switch.OFF) {
System.out.println("It is off!");
}
switch (s) {
case OFF.toString():
System.out.println("It is off!");
break;
}
}
}

But I got error on case OFF.toString()
Incompatible types. Found: 'java.lang.String', required: 'com.testInterfaces.dos.TestClass.Switch'

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

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

Post by admin »

If you are switching on an enum, each case must be a value in that enum, right? Why are you using case OFF.toString()?

I would suggest you to through a good book to learn the theory before attempting mock exams.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 33 guests