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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:27:13+00:00 2026-05-19T23:27:13+00:00

I wrote an abstract class to contain all rules of the application because I

  • 0

I wrote an abstract class to contain all rules of the application because I need them almost everywhere in my application. So most of what it contains is static final variables, something like this:

public abstract class appRules
{
    public static final boolean IS_DEV = true;
    public static final String CLOCK_SHORT_TIME_FORMAT = "something";
    public static final String CLOCK_SHORT_DATE_FORMAT = "something else";
    public static final String CLOCK_FULL_FORMAT = "other thing";
    public static final int USERNAME_MIN = 5;
    public static final int USERNAME_MAX = 16;
    // etc.
}

The class is big and contains LOTS of such variables.

My Question:

  • Isn’t setting static variables means
    these variables are floating in memory all the time?
  • Do you suggest insteading of
    having an abstract class, I have a
    instantiable class with non-static
    variables (just public final), so I
    instantiate the class and use the
    variables only when I need them.
  • Or is what am I doing is completely
    wrong approach and you suggest something else?
  • 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-19T23:27:14+00:00Added an answer on May 19, 2026 at 11:27 pm

    Isn’t setting static variables means these variables are floating in memory all the time?

    Yes. But unless, you have a really, really large number of constants like that, it isn’t going to make much difference. Besides, if you really need them to be named constants, you cannot really improve on your current approach.

    Do you suggest instead of having an abstract class, I have a instantiable class with non-static variables (just public final), so I instantiate the class and use the variables only when I need them.

    No. That won’t take any less memory. The JVM would have to keep the String objects corresponding to the literals anyway, so that it can assign them to the (non-static) variables when an instance of the class was created.

    Add to this the possibility that you may create multiple instances of the classes, which uses more space and consumes more CPU cycles.

    Or is what am I doing is completely wrong approach?

    I don’t think so.


    @Peter Lawrey points out that there are theoretical limits on the number of static fields that a class can have. The limiting factors include:

    • the size of the bytecode segment for the Cinit pseudo-method (2G bytes) – JVM spec 4.7.3
    • the number of fields in a class (64K) – JVM spec 4.5
    • the number of string literals in a class (64K) – JVM spec 4.4.3

    However, this is unlikely to be a practical problem. I cannot imagine a program really needing so many constants that the limits come into play. Besides, you could just split the constants over multiple classes.

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

Sidebar

Related Questions

I wrote a non-static Generic class instantiator for my Abstract Factory design, and use
I wrote an abstract class import javax.xml.bind.annotation.*; public abstract class Parent { @XmlAttribute(name =
i have wrote the following polymorphic code: public abstract class A { readonly int
I have a web service project that contains 3 classes: 1 Abstract public class
I would like to write an abstract base class class func_double_double_t : public unary_function<double,
Given the scenario... interface IBase{ void Process(int value); } abstract class Base : IBase
I have a base class DomainObject for all my business objects I am using
I have a relatively simple abstract class. I've simplified it further for this question.
I have an interface IServiceInfo and an abstract class ServiceInfo. There are several classes
I have this class: public abstract class Directory { protected int id; protected File

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.