没有公共私有或受保护的变量是什么?

如果不是:

private JButton theButton; 

我做:

 JButton theButton; 

有什么不同?

谢谢

包。 它们在同一个包中的其他类中可见。

FWIW,我通常在这些上使用我自己的无操作@Package注释,只是为了清楚地表明我知道自己在做什么 – 我不会忘记一些事情。 尽管这是默认设置,但是高质量代码中的包访问可能比其他三种可能性中的任何一种都少 – 有一个很大的例外:

在某些样式的unit testing中,希望能够访问通常为私有的方法或字段。 提供访问权限的一种方法是将它们设置为包访问,并将unit testing类放在同一个包中(但通常放在不同的“test”目录树中)。 一些开发人员认为这是不好的做法 – 一般来说,在测试中使用私有(或测试包)方法是不好的。

在Java中,有公共,受保护,包(默认)和私人可见性; 从最明显到最少排序。

如果未指定,则默认情况下可见性为

 package mytest.myvisibility; public class MyClass { public int myPublicInt; // visible to all protected myProtectedInt; // visible to subclasses of MyClass and to other members of the mytest.myvisibility package int myPackageInt; // visible only to other members of the mytest.myvisibility package private int myPrivateInt; // visible only to MyClass objects. } 

与c上的静态函数相同。 仅在同一文件上可见。