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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:11:51+00:00 2026-06-13T22:11:51+00:00

I currently define a class for constants like this public final class DataContainer{ public

  • 0

I currently define a class for constants like this

public final class DataContainer{

    public static final String x1="a";    
    public static final String x2="b";
     ...
    public static final String xn="z";    

}

& use these simply by accessing like this : DataContainer.x1

But now I need to define two implementation of this constants file(DataContainer1 & DataContainer2) & choose during runtime which of the two implementation class of constants to use throughout the application, without changing everywhere from DataContainer1.x1 to DataContainer2.x1.

How can I implement such a design ?

  • 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-13T22:11:52+00:00Added an answer on June 13, 2026 at 10:11 pm

    You should probably move away from a static final implementation of your constants.

    The main advantage of having static/final constants in the first place is that the compiler has access to these values already at compile time, and can thus perform various optimizations.

    But since you need to be able to exchange the actual values of the constants at runtime, that advantage will be gone anyway. Strictly speaking, one could argue that the values are no longer constants in the strictest sense, since they are actually variable.

    I would probably just go for a very simple and classic OO construction, where you have a map-like interface to the values and different implementations thereof.

    public interface Constants {
        public String get(String value);
    }
    

    (As a matter of fact, switching to a java.util.Map or to java.util.Properties may already do what you’re looking for.)

    Of course you may still want to define the values in the individual implementations of that interface as static final.

    If you’re absolutely in love with static access, you could use a Factory pattern to retrieve the actual instance of the constants at runtime. That will also totally make you look like a certified Java guru amongst your coworkers 😉

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

Sidebar

Related Questions

Say I've got the following: <?php abstract class MyParent { public static $table_name; public
Possible Duplicate: Static members class vs. normal c-like interface I am looking at somebodies
I would like to define a class constant using a concatenation of an existing
In my application, I have the following Constants class public class Constants { ...
I have a WPF Window defined in XAML like this: <Window x:Class=com.some.company.window xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I've created a custom module (which currently only defines a new Exception class), and
Our database currently doesn't define primary keys on any tables. All of the id
Currently the HashSet<T> constructor that allows you to define your equality comparison yourself is
I'm currently running a c++ project, with a class called Relation. I'm trying to
Sometimes my application generates URLs that look like http://localhost/?Area= or http://localhost/SomeController?Area= I don't currently

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.