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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:32:11+00:00 2026-06-08T04:32:11+00:00

Well, I’ve a final property but I don’t want to initialize it when I

  • 0

Well, I’ve a final property but I don’t want to initialize it when I create my object because I can’t. So I tried to not initialize it in my constructor but using a setter, I guessed it would have been something like a only-one-time usable setter, but I’ve this error :

Test.java:27: error: cannot assign a value to final variable foo

    this.foo = new String(foo);

Here is a short code I used to test this :

class Test {

    private final String foo;

    public static void main(String[] args) {
        Test test = new Test();
        test.setFoo("gygygy");
        System.out.println(test.getFoo());
    }

    public Test() {
        System.out.println("Constructor");
    }

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

}

So I assume the constructor implicitly makes something like this.foo = new String(); or this.foo = null; and I think I can’t modify this behavior, but how can I have an equivalent to what I wanna do ? I think in something like :

private String foo;

/* ... */

public void setFoo(String foo) {
    if( !(this.foo.isInitialized()) )
        this.foo = foo;
}

but the Object.isInitialized() method obviously doesn’t exist, and I can’t find an equivalent x)

So here’s my question in a few words : How can I do ? I want a final attribute that is not initialized at the instantiation of the object.

Thanks !

  • 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-08T04:32:14+00:00Added an answer on June 8, 2026 at 4:32 am

    You can add a boolean field isInisialized but the best option is to pass the value in the constructor (possibly using a Builder pattern if you need to)

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

Sidebar

Related Questions

Well, my website can not redirect to https://www.facebook.com/QuaFootSpa from http://quafootspa.com/ I have tried redirection
Well, that might be a strange question, and maybe just because I'm not familiar
Well, this must be a silly one. Here below is a can-not-be-simpler code in
Well, it seems simple enough, but I can't find a way to add a
Well, adding it will be no problem, but how can I generate the cache
well i want an enum i can use in switch function. i need to
Well I used css3pie in asp.net which is not working. I tried every possible
Well, i want to setting a button with setBackground(), but after this update i
Well I can start the thread when I press togglebutton to ON and it
Well, the title pretty much states it. I want to be able to draw

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.