-5

In Java string classes are final, we know that we can't inherit a final class and not able to write on this, then how did we enter a value on a string even its a final class?

2 Answers 2

6

A final class cannot be inherited from.

Creating an instance of a class is not the same thing as inheriting from it. So you can do it, even though it's final.

2

String is immutable, which means you can't modify it once it has been created. But of course you can choose how the string is created.

String has (as of Java 14) 16 constructors that all create string objects with values. You just call one of those.

In addition, there are 15 static methods on String that also create new strings, by calling some constructor (possibly a private one not included in the 16 above).

1
  • should probably add that the inheritence and the class being final is not strictly related to constructing values Commented Mar 9, 2020 at 10:24

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.