开发者

Is there any way to make a top level class private?

From what I know, we can only make inner classes private开发者_Python百科.

Is there any way to make a top level class private in java?


No, as specified in JLS:

Not all modifiers are applicable to all kinds of class declarations. The access modifier public pertains only to top level classes (§7.6) and to member classes (§8.5, §9.5), and is discussed in §6.6, §8.5 and §9.5. The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration (§8.5) and are discussed in §8.5.1. The access modifier static pertains only to member classes (§8.5, §9.5). A compile-time error occurs if the same modifier appears more than once in a class declaration.


If a class was private then it would be unusable from any other classes even from the same package. The private class may as well not exist.


Nope, it'd be a pretty pointless class if this was the case since no-one could access it!

EDIT: Well, there could potentially be ways in which it could be accessed through deserialisation and reflection, and potentially non-private inner classes (see Tom's comment.) However, none of these are really nice ways of going about it and it's a perfectly good (personally I think the best) choice that the JLS doesn't allow it to happen.


No, you can't do that. Think about it. If your outer class is private, who is it for? Nobody can access it and there won't be any use of the class at all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜