Search code examples
javamethodspackagevisibilitypublic-method

public methods in package-private classes


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

class SomePackagePrivateClass
{
    void foo();          // package private method

    public void bar();   // public method
}

Is there any practical difference in visibility between foo and bar here?


Solution

  • If the class is not going to be extended by another, more visible subclass*, the only difference is clarity of intent. Declaring all methods package private makes it more difficult for future readers to determine which of the methods are meant to be called by other classes in the same package.

    *which would not make much sense as a design solution to me, but technically is possible nevertheless.