Private Methods Over Public Methods

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

    Public methods are generally code that other classes which implement that class will want to use. Private methods are generally not as useful outside the class, or don't(alone) serve the purpose of what the class is meant to accomplish.

    Say you're in your IDE of choice, and you implement a some class A. Class A is only designed to do one thing, say document generation. Naturally you will have some mathematical and byte operation methods in Class A that are required to do document generation, but people trying to use Class A are not going to need these other methods, because they just want a document. So we make these methods private to keep things simple for any future users of our class.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-12 21:05

    The purpose of declaring a method private is to

    • hide implementation details
    • exclude the method from being listed as public API
    • make sure the logic behind the code is not used/misused externally
    • most of the time your method's execution depends on other methods being run before it; then you can also be sure that you control the correct sequence of using your method

    Use private for your methods unless you intend for your method to be safely used outside of the context of your class.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-12 21:09

    The only reason I could think of why private methods are useful is because it helps you from writing duplicate code.

    In addition to consolidating duplicate code (often expressed as "Don't Repeat Yourself" or "DRY"), use of private methods can also help you to structure and document your code. If you find yourself writing method which does several things, you may wish to consider splitting it into several private methods. Doing so may make it clearer what the inputs and outputs for each piece of logic are (at a finer granularity). Additionally, descriptive method names can help supplement code documentation.

    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题