Private Methods Over Public Methods

后端 未结 7 758
醉酒成梦
醉酒成梦 2020-12-12 20:26

I was examining the StringTokenizer.java class and there were a few questions that came to mind.

I noticed that the public methods which are to be used

7条回答
  •  [愿得一人]
    2020-12-12 21:10

    Making functions private gives you advantage in following cases :

    1. Making function private gives JVM compiler the option of inlining the function and hence boosting up the application performance
    2. If the class is inheritable and you extend it from a child class, then in case if you want to hide the functions from child class then you can do this (you can extend StringTokenizer).
    3. If a piece of code has to be used in multiple functions the you move that code in private utility method

提交回复
热议问题