0

I understand that a final method cannot be overridden. But when i need to use it in practice? Can smbd say me simple rule when i have to use it.

class A 
{
    final void m1() 
    {
        System.out.println("This is a final method.");
    }
}
Makc
  • 83
  • 6
  • Does this answer your question? [Why should I use the keyword "final" on a method parameter in Java?](https://stackoverflow.com/questions/500508/why-should-i-use-the-keyword-final-on-a-method-parameter-in-java) – chptr-one Dec 26 '22 at 10:42
  • @chptr-one no its about method parameter not about final method – Makc Dec 26 '22 at 10:51
  • There is no case in which you _have_ to use it. There are many cases in which using it is a good idea. – Louis Wasserman Dec 26 '22 at 14:05

1 Answers1

1

If you are coding smth for yourself, or a GUI application, for example, you'll probably never need to make your methods final. But, if you are making a library, and making some core functionality that you don't want to be changed by other programmers who will use it, you can use final keyword.

  • I understand that but problem is how understand what change would be wrong – Makc Dec 26 '22 at 09:15
  • @Makc lets assume that you are coding a server with some api. You can add plugins on this server that can add functionality to your server like a Minecraft servers. In class ```JavaPlugin``` there is a method ```getLogger``` which is final. I think is because this method is also used by server to print messages from plugin that it is loaded or enabled. – TheEntropyShard Dec 26 '22 at 13:57