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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:53:39+00:00 2026-06-02T19:53:39+00:00

I just noticed one curious case and wanted to see if someone will be

  • 0

I just noticed one curious case and wanted to see if someone will be able to explain it. Here is my case:

private enum Classifiers {
    NEURAL_NETWORK, NEAREST_NEIGHBOURS, IDENTITY;
}
private ClassifierInterface getClassifierInstance(Classifiers classifier) {
    switch (classifier) {
    case NEURAL_NETWORK:
        return new DoubleLayeredNeuralNetwork();
    case NEAREST_NEIGHBOURS:
        return new NearestNeighbours();
    case IDENTITY:
        return new IdentityClassifier();
    }
    return null; // If I comment out this line I get compilation error
}

See the comment. I would expect that Unreachable code error will be reported for this line. Instead I get Method must return value error if I comment out this line. However, there is no way the program flow will pass through there.

I even assumed it would be a guard case for the case of null value passed-in, but as expected this triggers NullPointerException for the switch condition.

I do not use switch very often, probably I am missing something here. Can somebody please try to help understand this behaviour?

  • 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-02T19:53:41+00:00Added an answer on June 2, 2026 at 7:53 pm

    That is correct behaviour as you do not have a default case statement. The problem is that you could add an value to the enum later and not re-compile the code which uses it. By forcing you to always handle when it is not one of the values, this is covered.

    BTW: classifier could be null which is another option switch doesn’t handle unfortunately.

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

Sidebar

Related Questions

I just moved from Visual Studio 2008 to Visual Studio 2010 (final) and noticed one major
I just finished a medium sized web site and one thing I noticed about
Just getting started with backbone.js, and one of the things I've noticed is that
i noticed that paypal displays a very different favicon, one that's not just a
I'm not quite sure I understand what Html.RenderAction() does. I've just noticed one weird
I just noticed that one of the things that keep working in browsers on
Hey guys I just noticed that my join statement here SELECT * FROM reports
I have just noticed one of my MSSQL databases has slowed down dramatically. The
I just recently noticed Dictionary.TryGetValue(TKey key, out TValue value) and was curious as to
I have a column version_num, in one of my tables, i just noticed that

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.