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 8034705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:06:14+00:00 2026-06-05T02:06:14+00:00

I have a crazy question about Java switches. int key = 2; switch (key)

  • 0

I have a crazy question about Java switches.

int key = 2;

switch (key) {
    case 1:
        int value = 1;
        break;
    case 2:
        value = 2;
        System.out.println(value);
        break;
    default:
        break;
}

Scenario 1 – When the key is two it successfully print the value as 2.

Scenario 2 – When I’m going to comment value = 2 in case 2: it squawks saying the The local variable value may not have been initialized.

Questions :

Scenario 1 : If the execution flow doesn’t go to case 1: (when the key = 2), then how does it know the type of the value variable as int?

Scenario 2 : If the compiler knows the type of the value variable as int, then it must have accessed to the int value = 1; expression in case 1:.(Declaration and Initialization). Then why does it sqawrk When I’m going to comment value = 2 in case 2:, saying the The local variable value may not have been initialized.

  • 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-05T02:06:15+00:00Added an answer on June 5, 2026 at 2:06 am

    Switch statements are odd in terms of scoping, basically. From section 6.3 of the JLS:

    The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement.

    In your case, case 2 is in the same block as case 1 and appears after it, even though case 1 will never execute… so the local variable is in scope and available for writing despite you logically never “executing” the declaration. (A declaration isn’t really “executable” although initialization is.)

    If you comment out the value = 2; assignment, the compiler still knows which variable you’re referring to, but you won’t have gone through any execution path which assigns it a value, which is why you get an error as you would when you try to read any other not-definitely-assigned local variable.

    I would strongly recommend you not to use local variables declared in other cases – it leads to highly confusing code, as you’ve seen. When I introduce local variables in switch statements (which I try to do rarely – cases should be very short, ideally) I usually prefer to introduce a new scope:

    case 1: {
        int value = 1;
        ...
        break;
    }
    case 2: {
        int value = 2;
        ...
        break;
    }
    

    I believe this is clearer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question that is driving me crazy. It is all about updating,
I just have a little question about a query making me crazy. I'm working
I have 3 question about TransactionScopeOption.Required that driving me crazy and I cant find
I have a crazy request about restarting tomcat server through my webapp. I was
I'm newish to the python ecosystem, and have a question about module editing. I
I have a question about Google Fonts. I am using the font Lato from
I have one question about how to handle a webform sites, i have many
This is a bit of a crazy question, but does anyone out there know
i have this crazy label for each time i create a migration that use
I have a huge crazy scene in my story board that has 36 different

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.