-2

I checked all over the internet if you could reassign final variables in the constructor in java, and everywhere it said it were possible. Yet for me, in eclipse June 2019, it gives me an error when trying to do so:

Here what it gives me

Thank you for your help!

Here is the same code, as text:

public class FinalClass {
  final int n = 5;
}

class InheritedClass extends FinalClass {
    {
        n = 6;
    }
}

which when compiled from command line produces this error:

C:\stuff>javac FinalClass.java
FinalClass.java:7: error: cannot assign a value to final variable n
        n = 6;
        ^
1 error
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • 2
    Post the links to "everywhere". – chrylis -cautiouslyoptimistic- Aug 27 '19 at 16:07
  • Why not just remove the first assignment if you intend for the constructor to reassign the value anyway? – Tim Biegeleisen Aug 27 '19 at 16:07
  • @chrylis One example where this aspect was used: https://stackoverflow.com/questions/5093744/initialize-a-static-final-field-in-the-constructor –  Aug 27 '19 at 16:09
  • You just posted a link to a question about "why *doesn't* this work?" – chrylis -cautiouslyoptimistic- Aug 27 '19 at 16:10
  • when it's said you can do that i don't think they mean in a subclass. – Nathan Hughes Aug 27 '19 at 16:10
  • What I meant by everywhere it said it were possible was that I saw a lot of usage of reassignment of final variables in the constructor. Sorry if I formulated it in a misleading way.. –  Aug 27 '19 at 16:11
  • by the time your inherited subclass constructor runs the constructor for the Final class has already finished. btw please add the code as text, not as a picture. – Nathan Hughes Aug 27 '19 at 16:12
  • @Nathan Hughes Good point, maybe I should've included it in the picture but it doesn't work in the superclass either. If you want I can add that –  Aug 27 '19 at 16:12
  • @Nathan Hughes The picture is just to show the error pop-up –  Aug 27 '19 at 16:12
  • @Tim Biegeleisen So the problem is that you cannot assign it in a constructor if it already has a default value? –  Aug 27 '19 at 16:14
  • @mdre Well what do you think the `final` keyword is implying? I mean, if you _could_ reassign it, then it would not really be final, right? – Tim Biegeleisen Aug 27 '19 at 16:15
  • @Tim Biegeleisen True, yet since it can be reassigned in the parent constructor I don't see why it couldn't be in the child one's either.. –  Aug 27 '19 at 16:18
  • that's because the parent needs to be able to rely on being able to have some things that the superclasses can't break. the fragile base class problem comes when the base class doesn't specify its contract sufficiently. and not being able to make things final prevents the base class from being able to limit its contract. https://stackoverflow.com/questions/2921397/what-is-the-fragile-base-class-problem – Nathan Hughes Aug 27 '19 at 16:24

1 Answers1

3

if you could reassign final variables in the constructor in java, and everywhere it said it were possible

They do say that you can (actually: must) assign to a final field exactly once from the class's own constructor.

They do not say you can reassign a final field, and they do not say you can do it from a subclass constructor. They don't say that, because you cannot.

If you want the value to be optionally specified by a subclass, you need a protected constructor that subclasses can call. And since you cannot reassign a value, you need to write a no-arg constructor to set the default value.

public class FinalClass {
    final int num;

    public FinalClass() {
        this.num = 5; // or: this(5);
    }

    protected FinalClass(int num) {
        this.num = num;
    }
}

public class InheritedClass extends FinalClass {
    public InheritedClass() {
        super(6);
    }
}
Andreas
  • 154,647
  • 11
  • 152
  • 247