0

I write a normal class

public class TestAccess {
}
class T{
    private int i=0;
        int j=0;
    protected int k=0;
    public int m=0;
}
class TT
{
    public void m(){
        T t= new T();
        t.j=9;
    }
}

Then I access it in the same directory with another class

   public class TestProtected extends T  
{

    public void method(){

    System.out.println(k);
    }
    public static void main(String[] args) 
    {
        System.out.println("HW!");
    }
}

But when I use package in the second class, there is something wrong to access class T;

   package m;
    public class TestProtected extends T  
    {

        public void method(){

        System.out.println(k);
        }
        public static void main(String[] args) 
        {
            System.out.println("Hello World!");
        }
    }

Can you guys tell me how I can access class T in package m?

The issue here is not related with public, I have tried, to be more general, what if you only have class T instead of its java code? The situation is you can access the T class without package, but you cannot access it with package.

1 Answers1

0

You should make your class T public. Otherwise, it is not visible to classes in different packages.

You'll have to move T to a separate file, since you can't have multiple top level public classes in the same file.

Finally, your TestProtected class located in pacakge m should import the T class.

Eran
  • 387,369
  • 54
  • 702
  • 768