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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:27:29+00:00 2026-05-30T01:27:29+00:00

The following code gives an unreachable statement compiler error public static void main(String[] args)

  • 0

The following code gives an unreachable statement compiler error

public static void main(String[] args) {
    return;
    System.out.println("unreachable");
}

Sometimes for testing purposes a want to prevent a method from being called, so a quick way to do it (instead of commenting it out everywhere it’s used) is to return immediately from the method so that the method does nothing. What I then always do to get arround the compiler error is this

public static void main(String[] args) {
    if (true) {
        return;
    }
    System.out.println("unreachable");
}

I’m just curious, why is it a compiler error?? Will it break the Java bytecode somehow, is it to protect the programmer or is it something else?

Also (and this to me is more interesting), if compiling java to bytecode does any kind of optimization (or even if it doesn’t) then why won’t it detect the blatant unreachable code in the second example? What would the compiler pseudo code be for checking if a statement is unreachable?

  • 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-30T01:27:31+00:00Added an answer on May 30, 2026 at 1:27 am

    Unreachable code is meaningless, so the compile-time error is helpful. The reason why it won’t be detected at the second example is, like you expect, for testing / debugging purposes. It’s explained in The Specification:

    if (false) { x=3; }
    

    does not result in a compile-time error. An optimizing compiler may
    realize that the statement x=3; will never be executed and may choose
    to omit the code for that statement from the generated class file, but
    the statement x=3; is not regarded as “unreachable” in the technical
    sense specified here.

    The rationale for this differing treatment is to allow programmers to
    define “flag variables” such as:

    static final boolean DEBUG = false;
    

    and then write code such as:

    if (DEBUG) { x=3; }
    

    The idea is that it should be possible to change the value of DEBUG
    from false to true or from true to false and then compile the code
    correctly with no other changes to the program text.

    Reference: http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.21

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

Sidebar

Related Questions

The following code gives a compile error: public void method(List<String> aList) {} public void
In C++ the following code gives a compiler error: void destruct1 (int * item)
Ok, consider the following code: private const int THRESHHOLD = 2; static void Main(string[]
The following code gives a compiler error in C++: const double** x; const void**
In the following code, g++ gives this error : 1.cpp: In member function void
The following code gives me the error: The best overloaded method match for 'System.Xml.Linq.XElement.XElement(System.Xml.Linq.XName,
The following code gives you a compiler error, as you'd expect: List<Banana> aBunchOfBananas =
The following code gives segmentation error when compiled with pgf90 on the Linux system,
The following code gives me this error: Cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List'. How
int func(struct x{int a;}y); int main() { } The above code gives following error

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.