About Question enthuware.ocajp.i.v8.2.908 :
Moderator: admin
-
- Posts: 1
- Joined: Fri Jan 01, 2016 5:49 am
- Contact:
About Question enthuware.ocajp.i.v8.2.908 :
The correct answer is "It will not compile." not the "Exception at run time."
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.908 :
I see that "It will not compile" is indeed marked as the correct answer. Please let me know if you see something else.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sat Aug 21, 2021 12:21 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.908 :
Hi, thank you for the great work. There is a bit misleading term in the explanation:
Item 4. All case labels should be COMPILE TIME CONSTANTS. You cannot have variables as case labels.
The highlighted sentence is a bit misleading. As you can have final variables with values evaluated at compile time as a case labels.
Try the following code
or run it directly from https://www.online-java.com/p1Bs95m8Yt
Suggestion: add clarification to the 2nd sentence
4. All case labels should be COMPILE TIME CONSTANTS. By either being a Literal value. eg: 50 or a Final variable whose value is evaluated at compile time. eg: final int someLabel = 5 * 2
Thank you.
Item 4. All case labels should be COMPILE TIME CONSTANTS. You cannot have variables as case labels.
The highlighted sentence is a bit misleading. As you can have final variables with values evaluated at compile time as a case labels.
Try the following code
Code: Select all
public class Main {
public static void main(String args[]) {
int full = Integer.parseInt("20");
// Next line causes compilation error because half's value cannot be evaluated at compile time!
// final int half = full/2;
// Next line causes compilation error because half is not final!
// int half = 10;
// Next line works perfectly! A final variable with value evaluated at compile time!
final int half = 5 * 2;
switch(full/2){
case half: System.out.println("Final variable can work with case label!");
}
}
}
Suggestion: add clarification to the 2nd sentence
4. All case labels should be COMPILE TIME CONSTANTS. By either being a Literal value. eg: 50 or a Final variable whose value is evaluated at compile time. eg: final int someLabel = 5 * 2
Thank you.
Last edited by dev_khaled on Sat Aug 21, 2021 6:38 am, edited 1 time in total.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.908 :
You are right.
Explanation has now been enhanced.
thank you for your feedback!
Explanation has now been enhanced.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 42 guests