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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:54:16+00:00 2026-05-28T04:54:16+00:00

In the following method, the compiler complains about a missing return statement even though

  • 0

In the following method, the compiler complains about a missing return statement even though there is only a single path through the method, and it contains a return statement. Suppressing the error requires another return statement.

public int foo() {
    if (true) {
        return 5;
    }
}

Given that the Java compiler can recognize infinite loops, why doesn’t it handle this situation as well? The linked question hints, but doesn’t provide details for this specific case.

  • 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-28T04:54:17+00:00Added an answer on May 28, 2026 at 4:54 am

    JLS 14.21, Unreachable Statements is the section that deals with this:

    The if statement, whether or not it has an else part, is handled in an unusual manner. For this reason, it is discussed separately at the end of this section.

    Ultimately it has to do with how conditional compilation is handled. Consider this method:

    public int foo() {
        if (DEBUG) {
            return 5;
        }
    }
    

    If DEBUG is static final boolean true; you might think the compiler should be smart enough to realize the method will always return 5. But if it’s changed to false, the code is no longer valid.

    The method must be valid for all paths through the method without a source code change, allowing optimizing compilers to omit bytecode without source modifications regardless of the flag’s value.

    The very end of the linked JLS section goes in to significant detail.

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

Sidebar

Related Questions

In Python compiled regex patterns have a findall method that does the following: Return
In the following example the compiler complains on the line: handler.handle(info); with the error:
The short version: I try to compile MAGMA and get complains about missing symbols:
The compiler is giving me the following complaints about the following class: class AguiWidgetBase
The following method does not compile. Visual Studio warns An out parameter may not
I have a the following method definition in my class: virtual Calc* Compile( Evaluator*
The following method does not work because the inner block declares a variable of
The following method, when called with something like String val = getCell(SELECT col FROM
The following method is launched from the constructor of UserControl. A cross thread exception
The following method does not apply the wpf changes (background = red) until 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.