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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:10:47+00:00 2026-06-13T10:10:47+00:00

what i basicly want is this: public class Test { private static final Integer

  • 0

what i basicly want is this:

public class Test 
{    
   private static final Integer a;

   public Test(Integer a) 
   {
      this.a = a;
   }

}

This obviously doesn’t work, cause the 2nd created instance would try to override the final variable.
So is there a way to give all the instances the same immutable value via the constructor?

  • 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-13T10:10:49+00:00Added an answer on June 13, 2026 at 10:10 am

    Static final values should be initialized in a static context, not by instances.

    One options is to set the value in the declaration:

    private static final Integer a=FileConfig.getInstance().getA();
    

    Each class can have a static {} block where code is called to initialize the static parts of the class.

    static {
        a = FileConfig.getInstance().getA();
    }
    

    Finally, you can set the value from a static method

    private static int getA() {
        return FileConfig.getInstance().getA();
    }
    
    private static final Integer a=getA();
    

    In closure, static instance initialization does not belong in instance constructors.

    If the configuration values change sometimes, there is simply no reason to store the value a in a static final variable. If you want to create each instance with the constant a in the constructor, what is the purpose of a static field in the first place? Somehow, when you call the constructor for the first time, you are passing in a value from somewhere. If the value deserves to be static and final, you can acquire it from within the static initializer. If the configuration is not a singleton, but every instance always produces the same value of a, you could easily do a = new FileConfig().getA();.

    Other than that, you could make the value non-final, and rest assured that since you always put in the same value of a, the static variable will not change.

    Still, you could make a a final instance variable of the class, set in the constructor.

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

Sidebar

Related Questions

I have a public EJB class that I want accessible online as a web
I have: WriteableBitmap bmp; I basicly want to save it into a file on
I have a issue with my listview, I basicly want the text in the
Basicly I want a text to resize depending how long the text is. If
Basicly, I want to write a C++ program and with it, pass messages to
I have defined a ConfigurationProperty class, which is basicly a key-value pair (with key
I have the following code in Main.java : public class Main extends Activity implements
I'm looking to do something like this in Java: public OuterClass { public innerClass
Basicly, i want to provide users to share my application's android market url. I
Having a confusing issue with jQuery, basicly this code is added to 2 elements,

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.