however, declaring x as final will give an error when initializing it in the constructor
To initialize static fields, use a static block .
And why should it be final ... The reason is that
- This is
private static and cannot be accessed externally. - If
final is not required then there is no need to do it static
So either remove static OR use final private static
Now, your other piece of code:
public void set(int x) { this.x = x; }
Questions:
- Fields
static should NOT be accessible with this .- Use a static block to initialize static fields.
Azodious
source share