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 !