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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:34:07+00:00 2026-06-06T02:34:07+00:00

I have an Android class which has a callback from an asynchronous HTTP process,

  • 0

I have an Android class which has a callback from an asynchronous HTTP process, and an enum with a number of status codes in:

public abstract class HttpPackage {

    public enum StatusCode {
        SUCCESS(0),
        NOT_FOUND(100),
        USERNAME_NOT_FOUND(101),
        AUTH_FAILED(110),
        SAVE_ERROR(111)
        //etc.

        private final int mCode;

        StatusCode(int i) {
            mCode = i;
            sByCode.put(i, this);
        }

    }

    private static final HashMap<Integer, StatusCode> sByCode = new HashMap<Integer, StatusCode>();

    //...
}

I’ve discovered the callback in the HttpPackage class is being hit before the enum constructs, which means that when I try to extract a status code from the static code map, it returns null, and my code thinks all the feeds are failing (when they aren’t). Why would this enum be constructing after the callback is hit?

  • 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-06T02:34:08+00:00Added an answer on June 6, 2026 at 2:34 am

    Classes are loaded lazily in Java. If you access the map before the StatusCode class is loaded, then of course it will be empty.

    The map should be in the enum itself. This way, if you’re accessing the map, you’re guaranteed that the enum class has been loaded, and that the map is not empty. The map should also be hidden from the outside code. You should provide a static StatusCode getByCode(int code) method in the enum.

    EDIT: example code:

    public enum StatusCode {
        SUCCESS(0),
        NOT_FOUND(100),
        USERNAME_NOT_FOUND(101),
        AUTH_FAILED(110),
        SAVE_ERROR(111);
    
        private final int code;
    
        private static final Map<Integer, StatusCode> map = new HashMap<Integer, StatusCode>();
    
        static {
            for (StatusCode sc : values()) {
                map.put(sc.getCode(), sc);
            }
        }
    
        StatusCode(int i) {
            this.code = i;
        }
    
        public static StatusCode getByCode(int code) {
            return map.get(code);
        }
    
        public int getCode() {
            return code;
        }
    
        public static void main(String[] args) {
            System.out.println(StatusCode.getByCode(111));
        }
    }
    

    Or you could also use a getMap() static method inside the constructor which lazily initializes the map if it’s null.

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

Sidebar

Related Questions

I am new to Android and Java programming. I have a class which implements
I have an Android app that has an Application class and I have another
I have an android app that plays audio from the application class. I have
I have two classes : import android.cla; public class CW { public static void
I have a simple spinner which has several number options. I want to just
My question is really short. I have a game which has a reset class
Java Code In Java code I have class called IdentificationResult which has 3 members:
My Android app has an Update class and I have, in the same app,
In my Android application, I have a DefaultApplication class which extends android.app.Application , and
I have set up a MapView Class which has Overlays (and they're working fine.)

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.