This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes using switch case? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "using switch case?" Watch "using switch case?" New topic
Author

using switch case?

maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186

compile time error???
answer is
constant expressions needed???x and y are not constants...
what does this mean??
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
the problem is with variable y: it should be a constant (that is should be marked final) and it should be a compile time constant which means that y should be declared and initialized at the same time.Refer to page 336 Chapter 5 of K & B .

so the correct code is :



SCJP 6.0 (86%) SCWCD 5 (94%)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

maggie you are not following the quoting of source rule properly. You've asked a lot of questions without a proper source (like this, this, this and this). Quoting source is not optional at javaranch. If you don't quote the source properly, then we might have to delete your questions...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
ok..i will do that from next post..actually i am trying lots of code on my own too.so its a combination of all that..anyways i will take care from next time....
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

maggie joseph wrote:ok..i will do that from next post..

No. Tell us what the source of the above question is now. As you've been told multiple times, it's required on JavaRanch.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

The case constant must be a compile time constant! Since the case argument has to be resolved at compile time, that means you can use only a constant or final variable that is assigned to literal value.

I think, this may help to you.!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Alpesh Rathod
Ranch Hand

Joined: Jan 06, 2009
Posts: 41
Abimaran Kugathasan wrote:The case constant must be a compile time constant! Since the case argument has to be resolved at compile time, that means you can use only a constant or final variable that is assigned to literal value.

I think, this may help to you.!



Very good very good...


Thanks,
Alps
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
question is from Examlab most probably.....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: using switch case?
 
Similar Threads
switch case basic
A Programming Interview Question
Confused about Dan's exam...
Inner class question from Dan's exam
changing a final variable