Java Package Level Access

I know that members of a default access control class may be available at the package level, but I'm confused about what access to the package level actually means. If access to the default elements can be obtained at the package level, then I should not be visible in the Test2 class in the following example? class 1 -

package pkg1; public class Test { int i=0; } 

class 2 -

 import pkg1.Test; public class Test2 { void get(){ Test t = new Test(); ti=0; } } 

Please help me get this concept. Thanks in advance.

+4
source share
2 answers

Access to the package level means that only classes defined in one package can access the package level variable. If you need to import the Test , then I guess that Test is in a different package, and therefore can not access the i .

For Test2 to access i , define it in the same package as Test1 .

+8
source

You forgot to write

 package pkg1; 

for class Test2.

Now it should work

0
source

All Articles