Private Methods Over Public Methods

后端 未结 7 772
醉酒成梦
醉酒成梦 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 20:56

    In order to add something, a private method can ALWAYS be changed safely, because you know for sure that is called only from the own class, no external classes are able to call a private method (they can't even see it).

    So having a private method is always good as you know there is no problem about changing it, even you can safely add more parameters to the method.

    Now think of a public method, anyone could call that method, so if you add/remove a parameter, you will need to change also ALL the calls to that method.

提交回复
热议问题