Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8853139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:34:31+00:00 2026-06-14T13:34:31+00:00

Why does my compiler get mad (Constant expression required) when I say: final static

  • 0

Why does my compiler get mad (Constant expression required) when I say:

final static char [] BASES = new char[]{'A', 'T', 'C', 'G'};
.
.
       char c= in.charAt(i);
            switch(c){
                case BASES[0] : break;
                case BASES[1] : packed =(char) (packed | 1); break;
                .
                .
                .
            }

but if I say:

final static char a ='A';
final static char t ='T';
         switch(c){
                case a : break;
                ...

it’s happy?
I feel like I’m being thick here. :-/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:34:33+00:00Added an answer on June 14, 2026 at 1:34 pm

    The argument to a case must be a constant, i.e. a primitive/string constant or literal or an enum constant. Your array is constant but not its content…

    In your case, an enum would be indicated and the code below is an example of how you could write it. It puts all the logic linked to the bases in the enum which you can now reuse where you need to – you can add methods too. And the main code is now clean and easy to read.

    Main code:

    public static void main(String[] args) {
        String input = "ATC";
        char packed = 0;
        for (char c : input.toCharArray()) {
            Base base = Base.valueOf(String.valueOf(c));
            packed = base.getPacked(packed);
        }
    }
    

    And your enum would look like:

    public enum Base {
    
        A {
            public char getPacked(char packed) {
                return packed;
            }
        }, T {
            public char getPacked(char packed) {
                return (char) (packed | 1);
            }
        }, C {
            public char getPacked(char packed) {
                return packed;
            }
        }, G {
            public char getPacked(char packed) {
                return packed;
            }
        };
    
        public abstract char getPacked(char packed);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does the compiler say a constant value is required for the first case...the
Possible Duplicate: Does the compiler continue evaluating an expression where all must be true
On VC++, why does the compiler only pad struct when non-char datatypes are used?
How does a SQL query work? How does it get compiled? Is the from
How does HTML work? Does it get parsed, compiled and then executed? Does it
What's going on when the assignment statement executed at Line 4 , does compiler
How does virtual functions work behind the scenes in Inheritance ? Does the compiler
I was wondering if there is a possible optimization where the compiler does not
In forward referencing language such as c#, how does the compiler handle this? What
When using plain C in iPhone development, does the compiler place the variables declared

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.