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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:34:57+00:00 2026-05-22T11:34:57+00:00

Possible Duplicate: Variable scope in a switch case I’ve got a code like this:

  • 0

Possible Duplicate:
Variable scope in a switch case

I’ve got a code like this:

switch(a) {
case b:
 Object o = new Object();
 return o;
case c:
 o = new Object();
 return o;
 }

and i’m interesting why it’s possible to use variable declared after first case label in the second one even if first state will never be reached?

  • 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-22T11:34:57+00:00Added an answer on May 22, 2026 at 11:34 am

    Despite being in different cases, the variables local to the switch statement are in the same block, which means they are in the same scope.

    As far as I know, new scope in Java is only created in a new block of code. A block of code (with more than one line) has to be surrounded by curly braces. The code in the cases of a switch statement is not surrounded by curly braces, so it is part of the whole statement’s scope.

    However, you can actually introduce a new scope to the statement by adding curly braces:

    switch (cond) {
    case 1:{
         Object o = new Object();
    }
        break;
    case 2:{
        // Object o is not defined here!
    }
        break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Returning reference to a local variable I happened to find this code
Possible Duplicate: C# member variable initialization; best practice? Is there any benefit to this:
Possible Duplicate: Finding the Variable Name passed to a Function in C# public new
Possible Duplicate: Variable scope in Javascript for loop for(i=0;i<4;i++){ } Do I need to
Possible Duplicate: matlab - variable in plot title I would like to quote the
Possible Duplicate: What does the variable $this mean in PHP? I know this is
Possible Duplicate: Limiting variable scope Is there a way to force R to ignore
Possible Duplicate: Difference between pointer variable and reference variable in C++ I saw this
Possible Duplicate: Use a variable within a variable? Java Hi this is a general
Possible Duplicate: Why do I see a double variable initialized to some value like

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.