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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:36:37+00:00 2026-06-14T15:36:37+00:00

In the following code: private static void example() { String inputString = test; switch

  • 0

In the following code:

private static void example() {
    String inputString = "test";
    switch (inputString) {
        case "test":
            String outputString = "The string said test";
        case "not test":
            String outputString = "The string did not say test";
    }
    /*Do stuff with ouputString*/        
}

Eclipse highlights the line after Case "not test" with the error Duplicate local variable outputString. However, since they are in separate branches of the switch statement, they are not actually in conflict, as only one can ever be in effect at a time.

Obviously, in this case I could get around the problem by moving the outputString declaration outside the switch statement. However, in more complex programs, the solution might not be so simple.

Is this simply an error thrown by Eclipse to prevent poor programming technique, or an actual Java error?

I’m very new to Java, so apologies if this is a newbie question – I’ve had a google for “Duplicate local variable Java”, but so far all that turned up were people asking for help with fixing the problem (the usual solution being to rename one of the variables), not discussion of the problem itself.

  • 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-14T15:36:38+00:00Added an answer on June 14, 2026 at 3:36 pm

    The scope of a variable in each case of a switch statement in java is the whole switch statement.

    You can create further nested scopes by adding braces like so:

     switch (inputString) {
            case "test": {
                String outputString = "The string said test";
                break;
            }
            case "not test": {
                String outputString = "The string did not say test";
                break;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using the following code (C#): private static void PlayLoop(string filename) { Audio
I have the following code: private static final Set<String> allowedParameters; static { Set<String> tmpSet
I have the following code: public class Test extends JFrame implements ActionListener{ private static
How do you convert the following C# code to VB.NET? private static readonly ICollection<string>
I have the following code: private String foo; public void setFoo(String bar) { foo
The following code: public class TestInnerClass { public static void main(String[] args) throws IOException
Take as an example the following C# function: static void Main(string[] args) { var
I have the following code: public partial class queryTerm : System.Web.UI.UserControl { private static
The following code prints null once. class MyClass { private static MyClass myClass =
I currently have code that does the following: private final static ExecutorService pool =

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.