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

前端 未结 3 1430
被撕碎了的回忆
被撕碎了的回忆 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:38

    In Java there are public, protected, package (default), and private visibilities; ordered from most visible to the least.

    If you do not specify it, by default the visibility is package.

    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
    }
    

提交回复
热议问题