facebook

Improper code indentation on Java switch

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #266471 Reply

    jieryn
    Member

    I’m seeing a formatting error when ‘return’ is used within a switch statement. The next line, if it is a comment, is erroneously indented to the next level. Please find the smallest amount of code to demonstrate:

    public class SwitchTest
    {
      public static void main(String[] args)
      {
        final int a = 1;
    
        switch (a)
        {
          /* indentation */
          case 1:
            return; // <-- notice the return
    
            /* indentation */
          default:
            break;
        }
    
        final int b = 2;
    
        switch (a)
        {
          /* indentation */
          case 1:
            break; // <-- notice the break
    
          /* indentation */
          default:
            return;
        }
      }
    }
    

    Thanks!
    MyEclipse++

    #266563 Reply

    Riyad Kalla
    Member

    Moving to OT > Soft Dev

    I’m not sure I follow, everything under a case should be indented as content of that case statement (like an implied body). This code looks OK to me… maybe I’m missing something?

    #266585 Reply

    jieryn
    Member

    In switch #1 the comment on top of the default case is indented at a different level than the word ‘default’. However, in the second switch the indentation is at the same level as the word ‘default’. The only difference between the two switches is that in switch #1 the first case has ‘return’ instead of ‘break’; whereas, the second switch, which properly indents the comment on top of the 2nd case, default, uses a ‘break’ in the case statement above.

    I realize my description is a bit confusing. This is best summarized by ‘return’ vs ‘break’ causes different indentation of comments on subsequent case statements.. ‘return’ screws up the comment, whereas ‘break’ handles it properly.

    Hope this helps,
    MyEclipse++ 🙂

    #266587 Reply

    Riyad Kalla
    Member

    The indents all look the same to me in what you pasted… maybe I’m just not seeing it… please point out what I’m missing (I think I have my ‘dense as a rock’ hat on today or something…)

    Attachments:
    You must be logged in to view attached files.
    #266591 Reply

    jieryn
    Member

    Please place your magic red tape around the /* */ comment areas. I’m sorry if I misled you, I am concerned about the indentation of the comments. It is inconsistent depending on whether I break vs return on the previous case statement.

    #266596 Reply

    Riyad Kalla
    Member

    OH! I knew I was missing something, I see exactly what you mean now.

Viewing 6 posts - 1 through 6 (of 6 total)
Reply To: Improper code indentation on Java switch

You must be logged in to post in the forum log in