Search code examples
javafinal

Assigning a final field in Java


I am working on a class that has some fields with final access modifier like: final textField and I am allowed to assign to them forsome reason. When I change them to static final I can no longer assign anything to them (it complains that they are final like it should have done in the first place). Any ideas why this is happening ?

Example for the first case:

final LabelField label_title;

label_title = new LabelField(
        "Press the button to launch the speed test",
        LabelField.FIELD_HCENTER);

Solution

  • You can assign final fields in constructor, you can't assign static final fields in a constructor. You shouldn't change static fields in a constructor in any case.