Search code examples
javastringbuilderfinal

A final class and underlying consequences in Java


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?!


Solution

  • 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.