0

I heard this line somewhere and I cannot get it off of my mind:

"All members of a final class are implicitly final."

Now, I know very well these three famous concepts:

  1. A final class cannot be extended.
  2. A final variable cannot be re-assigned with a new value once initialized.
  3. A final method cannot be overridden.

But, if all members (variables, methods) of a final class are implicitly final, then we have a final class AND final variables AND final methods in it.

Then, how is it possible that StringBuilder, despite being final, allows its contents to change?!

  • 1
    final doesn't mean immutable (see `ArrayList` for example) –  Jan 17 '15 at 10:26
  • 3
    Only the methods are final implicitly (since they could only be overridden by subclassing and subclassing is denied). The variables remain non-final unless marked. – Dave Jan 17 '15 at 10:26
  • You cant override any methods in the StringBuilder class, only its text -- content can be changed, note the difference. – Biu Jan 17 '15 at 10:27
  • Write some sample code and run it. Learn more that way besides just remebering and site online - teachers are not perfect if someone really said that. It atint true – tgkprog Jan 17 '15 at 10:28
  • @Dave: Yes, that must be it! No wonder why "final" and "mutable" are not mutually exclusive! That makes sense now. It has got to be only methods that essentially end up being final due to a final class they belong to. A class being final allows NO sub-classing, and without sub-classing there can be NO overriding of methods. – Mohammad Ali Asgar Jan 17 '15 at 10:54
  • @RC: I don't understand what ArrayList exemplifies here. It is no-final and mutable. – Mohammad Ali Asgar Jan 17 '15 at 11:18
  • @MohammadAliAsgar I meant you can add content to a final arraylist –  Jan 17 '15 at 12:17
  • @RC: Yes, you can add contents to a final instance of ArrayList. That is a different thing. You are making a reference variable final which points to an instance of ArrayList. But here we are talking about a whole class that is final. – Mohammad Ali Asgar Jan 17 '15 at 17:26

2 Answers2

2

That assertion is oversimplified and therefore wrong. Only methods of a final class can be considered implicitly final. This fact is however irrelevant in itslef, as methods of a final class cannot be overriden because there can't be any subclasses. It doesn't really matter if they're final or not, implicitly or explicitly.

Darkhogg
  • 12,286
  • 4
  • 20
  • 24
  • how is it "not exactly true"? – eis Jan 17 '15 at 10:32
  • @eis: It is, as far as I know, not stated in the Java Language Specification. But then, as I said, it's completely irrelevant, as the only difference between a final and non-final method is it being overridable. – Darkhogg Jan 17 '15 at 10:35
  • well, either it is or it isn't. You shouldn't claim so if you think it isn't true, it's a bit hard to judge from the answer which side are you on. I agree on it being irrelevant though. – eis Jan 17 '15 at 11:09
  • @eis, you're right, I've changed the wording to a more neutral one. – Darkhogg Jan 17 '15 at 11:13
1

A class can be final, but mutable.

The class being final just means there can be no subclasses, but says nothing about the behaviour of the class.

Conversely, fields and methods can be final (and the instances immutable), but the class not final.

Finality of a class and of class members are unrelated.

Bohemian
  • 365,064
  • 84
  • 522
  • 658
  • 1
    All the methods in a final class are implicitly final as well, and methods are also members of the class. Other than that I agree. – eis Jan 17 '15 at 10:30