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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:03:18+00:00 2026-05-17T16:03:18+00:00

I think I don’t understand how the scope works in a switch case. Can

  • 0

I think I don’t understand how the scope works in a switch case.

Can someone explain to me why the first code doesn’t compile but the second does ?

Code 1 :

 int key = 2;
 switch (key) {
 case 1:
      String str = "1";
      return str;
 case 2:
      String str = "2"; // duplicate declaration of "str" according to Eclipse.
      return str;
 }

Code 2 :

 int key = 2;
 if (key == 1) {
      String str = "1";
      return str;
 } else if (key == 2) {
      String str = "2";
      return str;
 }

How come the scope of the variable “str” is not contained within Case 1 ?

If I skip the declaration of case 1 the variable “str” is never declared…

  • 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-17T16:03:19+00:00Added an answer on May 17, 2026 at 4:03 pm

    I’ll repeat what others have said: the scope of the variables in each case clause corresponds to the whole switch statement. You can, however, create further nested scopes with braces as follows:

    int key = 2;
    switch (key) {
    case 1: {
        String str = "1";
        return str;
      }
    case 2: {
        String str = "2";
        return str;
      }
    }
    

    The resulting code will now compile successfully since the variable named str in each case clause is in its own scope.

    Also note that the scope for case: (sans-{}) goes to the ‘global’ switch scope. And after that every other case scope (with or without {}) will receive those variables in a way uncommon for nested java scope, they can’t be overridden. So, for example, adding {} only to the second case would still give you the same error as it would get the variable from the first one injected, but adding braces to the first one would compile. I am not sure this is defined as such on the spec, but it what happens on most implementations.

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

Sidebar

Related Questions

I have an MDI form as the app start object. I don't think is
So I want to get PHUnit up so I can run selenium server, but
I'm working on an App that will allow user's to edit the same database
I have a rather large tree grid of clients (total tree size 3000 but
While developing a website, firebug threw me a javascript error linking to the execution
Just wondering if anyone could help me with this. I'm new to actionscript, and
I'm getting the following warnings when trying to initiate a session: Warning: session_start() [function.session-start]:
I would like to have an application/script that I could use to execute a
I am interested in developing apps for feature phones, since a large percentage of
I'm not sure if I am implementing the scroll view incorrectly, or if the

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.