Declaring immutable class final
saves programmer from the need to repeat declaring final in each and every method declaration, over and over and over again.
In classes like java.lang.String, having over 60 methods, this is substantial save, as well as important guarantee that necessary modifier won't be omitted by mistake.
When object is intended to be immutable, mistakes to declare final method may be hard to detect, because there is no reliable way to tell whether programmer omitted final modifier intentionally or by mistake.
final classes have no real purpose except perhaps brevity
Besides brevity and helping to avoid mistakes, a strong benefit of declaring immutable class final is that this makes programmer's intent explicit, unambiguously communicating to API users that none of class methods are intended for override.
Alternative way, that is, declaring all methods final, lacks this important "feature", as it leaves users of the API in the indecisive state, whether API designers intended to only protect some methods from overloading, and it only accidentally turned out that all of them got final, or there was a design decision to cover all the class.
my opinion is that final classes are superfluous as final methods are all you need
Given above, having final modifier for class doesn't look superfluous to me.
It is worth noting that Sun/Oracle Java tutorial presents final classes as a matter of usefulness and not as that of convenience. If final classes purpose was mere brevity / syntactic sugar, one would expect tutorial to explain these as convenience.
...you can also declare an entire class final. A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String
class.
Such methods shouldn't be static
(what if you have to deal with two databases?) and they also shouldn't be on your entity classes.
Instead, you should have a separate type, that represents the storage for this entity. That type could be generic, so that code that's not entity-specific doesn't have to be repeated.
This way, inheritance works (storage for ClassA can inherit from some base storage) and you don't have to perform the nonsensical "I have to create a person to get a list of all persons".
This pattern is for example used by Entity Framework, there the storage type is called DbSet<T>
.
Best Answer
Methods which are not attached to any specific class like these are called "free functions", and as you have adequately noted, OO does not provide for them, and some languages also do not provide the facilities for them.
In a language like C++ you can just place the function in a namespace. But if you're working in Java or C# you must pointlessly place them in a class and make them a static member for no good reason at all.