Re: static final int, but constructor with (final int) arguments
Merciadri Luca wrote:
I've got a class constructor which takes two final int arguments; they
come from the CLI and this one is parsed only once, at the beginning
of the programming. Once they have been parsed from the CLI, errors
handled, etc., they never change.
What do you mean by "the beginning of the programming"?
During the program's execution, I need to create two objects from this
class. These objects thus inherit some variables that need to be static final
int. However, the final is not accepted because, in the constructor, I
evidently define one of these variable's values.
That's not inheritance.
Why do they need to be final?
To take an example, consider the class foo. You know that
==
public class foo {
*TYPE NAMES SHOULD START WITH AN UPPER-CASE LETTER!*
private static final int c;
public Foo(final int a, final int b) {
c = 2*a;
}
}
==
will produce an error because c cannot be modified. I can understand
it, and I thus tried to put the final keyword in the constructor, just
before `c', but to no avail. I tried various combinations, using
static blocks, etc., but nothing more.
Modified? Heck, you don't even initialize it, another error.
Do you have any reasonable way to achieve this?
It doesn't make any sense to modify a final *static* variable from the
instance. If you need to do that, then the variable isn't final.
What do you really want to do?
If you want something that is final *for that instance*, then it is
part of the *instance* state, not the class state. Why would the
whole class care about something that matters only to an instance?
Try this:
public class Foo
{
private final int c;
public Foo( final int a, final int b )
{
c = 2*a;
}
}
--
Lew