public methods in package-private classes

后端 未结 7 1601
刺人心
刺人心 2020-12-04 19:35

Does it make a difference to mark methods as public in package-private classes?

class SomePackagePrivateClass
{
    void foo();          // pack         


        
相关标签:
7条回答
  • 2020-12-04 20:08

    It makes very little difference, unless the class is extended by a public (or protected nested) class.

    As a matter of consistency, I'd go for public. It should be rare for methods to be anything other than public or private.

    There are a number of examples of public methods in the package private java.lang.AbstractStringBuilder class. For instance length() is public in AbstractStringBuilder and is exposed in the public class StringBuilder (overridden in StringBuffer to add synchronisation).

    0 讨论(0)
提交回复
热议问题