class Foo {
final String s;
final int x = s.length();
Foo(String s) {
this.s = s;
}
}
The above code will not compile, because the assignment to x happens before the assignment to s. I could place the assignment to x inside the constructor, but for readability in a real case with 100 fields, I'd rather not.
I have come up with a workaround, which I dislike. Can you find a better one?
abstract class Hack {
final String s;
Hack(String s) {
this.s = s;
}
}
class Foo extends Hack {
final int x = s.length();
Foo(String s) {
super(s);
}
}