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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:21:47+00:00 2026-06-05T02:21:47+00:00

I have a class holding a large a mount of generated constants as such:

  • 0

I have a class holding a large a mount of generated constants as such:

public class Constants extends SomeBaseClass {

  // init() is defined in some base class...
  public static final XXX KEY1 = init(...);
  public static final XXX KEY2 = init(...);
  public static final XXX KEY3 = init(...);

  // ...
  public static final XXX KEY2000 = init(...);
}

When the number of generated constants is very high, this results in a static initialiser that is larger than the upper limit for Java method sizes (i.e. > 64kb), resulting in a compiler error. One solution is to create several “block initialisation methods” for blocks that can be guaranteed to produce less than 64kb of byte-code, such that they fit into a method:

public class Constants extends SomeBaseClass {

  public static XXX KEY1;
  public static XXX KEY2;
  public static XXX KEY3;

  // ...
  public static XXX KEY2000;

  static {
    initialise0001To1000();
    initialise1001To2000();
  }

  private static void initialise0001To1000() {
    KEY1 = init(...);
    KEY2 = init(...);
    KEY3 = init(...);
    // ...
  }

  private static void initialise1001To2000() {
    // ...
    KEY2000 = init(...);
  }
}

The drawback of this is that I can no longer declare the constants as final, because they are now no longer initialised directly in the static initialiser.

My question is, how can I circumvent that compiler / JVM limitation in a way that I can still generate static final constants?

  • 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-05T02:21:48+00:00Added an answer on June 5, 2026 at 2:21 am

    I finally went for a solution involving nested classes. This was suggested in a comment to this answer here by user Loadmaster. Nested classes have two advantages:

    • They allow for hiding these workaround implementation details from the outside world by being private nested classes
    • They allow for keeping constants final

    But they also have a disadvantage compared to templatetypedef’s solution:

    • I will run into the same problem again with much larger numbers of constants

    Right now, however, this seems to be the most suitable solution:

    public class Constants {
    
      public static XXX KEY1    = Constants1.KEY1;
      public static XXX KEY2    = Constants1.KEY2;
      public static XXX KEY3    = Constants1.KEY3;
    
      // ...
      public static XXX KEY2000 = Constants2.KEY2000;
    
      // Nested class holding 1000 constants
      private static class Constants1 extends SomeBaseClass {
        KEY1 = init(...);
        KEY2 = init(...);
        KEY3 = init(...);
        // ...
      }
    
      // Nested class holding the next 1000 constants
      private static class Constants2 extends SomeBaseClass {
        // ...
        KEY2000 = init(...);
      }
    
      // Keep generating nested classes for more constants...
      private static class Constants3 ... {}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class A: public B { ...} vector<A*> v; I want to do
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
So I have a class made specifically for holding an event handling method which
I have a class holding the list of contacts of my user in an
I have a class holding entries in a list, which can be cleared and
I have class like the following: class test { public string Name; public string
I have a templated class holding a value. Is it possible to add a
I have an abstract class holding a number of fields which are inherited by
I have class that is a DataContract and the member values are holding default
I have a class holding complex scientific computations. It is set up to only

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.