Non-public top level class in Java

2020-02-29 04:59发布

What's the reason of making top-level class non-public in Java?

Let's say we have Foo.java, there could be

class Foo {
}

or

public class Foo {
}

I understand that there will be some class - visibility issues with the former example (probably it won't be visible from other packages). But anyway, are there any reasons why someone may want to do as in the first code sample?

UPD: What cons I see in the former solution: nobody cares that it's non-public. That class can be simply extended by some other public class in the same package later, then, non-public part of the class may bring you visibility/access issues.

标签: java
5条回答
唯我独甜
2楼-- · 2020-02-29 05:28

Here is an example. No one needs to know about existence of our ConcreteDocument.

DocumentIF.java

public interface DocumentIF {
}

ConcreteDocument.java

class ConcreteDocument implements DocumentIF {
}

DocumentFactory.java

public class DocumentFactory {
    public DocumentIF createDocument() {
        return new ConcreteDocument();
    }
}
查看更多
倾城 Initia
3楼-- · 2020-02-29 05:41

I understand that there will be some class - visibility issues with the former example (probably it won't be visible from other packages).

That seems to me to be reason enough to use it if you want to keep the class private to that one package.


Just noticed another use! It seems you can only have one public top-level class per code file, but any number of non-public top-level classes. Haven't verified it personally, but if true that could be quite useful to prevent cluttering your project folder and to group classes with related functionality that aren't needed outside of the package.

查看更多
▲ chillily
4楼-- · 2020-02-29 05:42

It is considered good design to keep the visibility of a class to the most minimum required. The reasons that I can think of:

  1. The class can easily change in the future without causing breakages in external packages as the external packages do not have access to the class. In this regard it might be even better to start off a class by making it a private inner class.
  2. The class being package visible cannot be extended by classes in external packages. This again makes it easier for this class to change without causing breaking changes in external packages. If this class was not meant to be extended then it would be even better to make this final.
  3. A public visible class becomes a part of the exported API of your library. If you are a library designer, it is better to keep your exported API as small as possible because you do not want to confuse your consumer with un-necessary classes/details. Item 1 would again hold good in this case.

The book "Effective Java" by Josh Bloch is an excellent reference for Idiomatic Java code and design.

查看更多
爷的心禁止访问
5楼-- · 2020-02-29 05:46

Classes without a public or protected modifier are only visible inside the package they reside. If you think of components and interfaces there is a reason for leaving out the public modifier. Let's say you have a public class MyCompontent that internally uses other classes, but does not want to publish those to the outside world (users of the component) it makes sense to leave out the visibility modifier.

查看更多
女痞
6楼-- · 2020-02-29 05:49

Typically, you make a class package-private because you don't want the class to be used outside the package. When a top-level class isn't public, it's private to the package.

Say you have a package with a number of classes that must communicate the same sort of data with one another. But this data structure is an implementation detail and so you don't want it being used by user code. Making the transfer class package private maintains this sort of package level encapsulation.

查看更多
登录 后发表回答