Java: Subpackage visibility?

后端 未结 9 1489
予麋鹿
予麋鹿 2020-11-29 02:06

I have two packages in my project: odp.proj and odp.proj.test. There are certain methods that I want to be visible only to the classes in these two

9条回答
  •  感动是毒
    2020-11-29 02:49

    Without putting the access modifier in front of the method you say that it is package private.
    Look at the following example.

    package odp.proj;
    public class A
    {
        void launchA() { }
    }
    
    package odp.proj.test;
    public class B
    {
        void launchB() { }
    }
    
    public class Test
    {
        public void test()
        {
            A a = new A();
            a.launchA()    // cannot call launchA because it is not visible
        }
    }
    

提交回复
热议问题