Page 1 of 1
About Question enthuware.ocpjp.v7.2.1418 :
Posted: Thu Dec 18, 2014 4:12 pm
by SepticInsect
How do I know when to use Switch.OFF or just OFF?
Re: About Question enthuware.ocpjp.v7.2.1418 :
Posted: Thu Dec 18, 2014 8:25 pm
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.
Re: About Question enthuware.ocpjp.v7.2.1418 :
Posted: Fri Dec 19, 2014 1:16 am
by SepticInsect
Ok. Thanks!
Re: About Question enthuware.ocpjp.v7.2.1418 :
Posted: Sun Dec 20, 2015 6:34 pm
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. ...
Re: About Question enthuware.ocpjp.v7.2.1418 :
Posted: Mon Dec 21, 2015 3:05 am
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.
Re: About Question enthuware.ocpjp.v7.2.1418 :
Posted: Fri Jan 29, 2016 6:17 am
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?
Re: About Question enthuware.ocpjp.v7.2.1418 :
Posted: Fri Jan 29, 2016 9:29 am
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.
Re: About Question enthuware.ocpjp.v7.2.1418 :
Posted: Fri Jan 29, 2016 2:53 pm
by Sergiy Romankov
Thanks, very clear explanation.
Re: About Question enthuware.ocpjp.v7.2.1418 :
Posted: Tue Apr 13, 2021 11:53 pm
by alfredo.zuloaga
I tryed with
Code: Select all
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;
}
}
}
[code]
But I got error on case OFF.toString()
Incompatible types. Found: 'java.lang.String', required: 'com.testInterfaces.dos.TestClass.Switch'
Re: About Question enthuware.ocpjp.v7.2.1418 :
Posted: Wed Apr 14, 2021 2:07 am
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.