Private Methods Over Public Methods

后端 未结 7 759
醉酒成梦
醉酒成梦 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:07

    When writing clean code in Java or any other object-oriented language, in general the cleanest most readable code consists of short concise methods. It often comes up that the logic within a method could be better expressed in separate method calls to make the code cleaner and more maintainable.

    With this in mind, we can envision situations where you have many methods performing tasks towards a single goal. Think of a class which has only one single complex purpose. The entry point for that single goal may only require one starting point (one public method) but many other methods which are part of the complex operation (many private helping methods).

    With private methods we are able to hide the logic which is not and should not be accessible from anywhere outside of the class itself.

提交回复
热议问题