What are variables without public, private or protected declared as?

前端 未结 3 1417
被撕碎了的回忆
被撕碎了的回忆 2021-02-01 15:16

If instead of:

private JButton theButton;

I define a field like this:

JButton theButton;

What is the differen

3条回答
  •  粉色の甜心
    2021-02-01 15:49

    Package. They're visible to other classes in the same package.

    FWIW, I usually use my own no-op @Package annotation on these, just to make it clear that I know what I'm doing - that I didn't just forget something. Even though it's the default, package access is probably used less in high-quality code than any of the other three possibilities - with one big exception:

    In some styles of unit testing, it's desirable to be able to get access to methods or fields that are normally private. One way to provide access is to set them to package access, and put the unit test class in the same package (but usually in a different "test" directory tree). Some developers think that this is bad practice - that in general, it's bad to use private (or package-for-testing) methods in tests.

提交回复
热议问题