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

  • SEARCH
  • Home
  • 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 1023567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:38:38+00:00 2026-05-16T11:38:38+00:00

Why are different case bodies not automatically in their own scope? For example, if

  • 0

Why are different case bodies not automatically in their own scope? For example, if I were to do this:

switch(condition) {
  case CONDITION_ONE:
    int account = 27373;
  case CONDITION_TWO:
    // account var not needed here
  case CONDITION_THREE:
    // account var not needed here
  case CONDITION_FOUR:
    int account = 90384;
}

the compiler would complain about local variable redefinitions. I understand I could do this:

switch(condition) {
  case CONDITION_ONE: {
    int account = 27373;
  }
  case CONDITION_TWO: {
    // account var not needed here
  }
  case CONDITION_THREE: {
    // account var not needed here
  }
  case CONDITION_FOUR: {
    int account = 90384;
  }
}

to put a block around each set of statements to be executed to put each account variable in its own scope. But why doesn’t the language do this for me?

Why would you ever want to declare a local variable in CONDITION_ONE‘s body and then use it in CONDITION_TWO‘s? This seems like a TERRIBLE idea which should be explicitly banned, not implicitly permitted.

  • 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-05-16T11:38:38+00:00Added an answer on May 16, 2026 at 11:38 am

    That would be inconsistent with the rest of the language.

    As it is, scope is always determined by blocks. That sort of consistency makes Java easier to read and maintain.

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

Sidebar

Related Questions

I just read this question: Why are different case condition bodies not in different
The use case is that I use one development machine for different source trees.
I have the following case: Edited: removed link In IE7 gives me different view
I'll rewrite a different question of mine, because the problem case somewhat changed: If
Is there a way to use one CASE statement and return 2 different values?
How can I create two different lists for one model? In my case I
First I'll lay out what I'm trying to achieve in case there's a different
Can different EJBs deployed on the same container and works synchronously write their application
In my code I need to be able to jump (goto) a different case
I need to write handlers for several different case types (in Python). The interface

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.