4

I know that class members with default access control can be accessible at package level but i'm confused about what does package level access actually mean. If default members can be accessed at package level then shouldn't i be visible in class Test2 in 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();
    t.i=0;
}
}

Please help me getting this concept. Thanks in advance.

4

2 回答 2

8

包级访问意味着只有在同一个包中定义的类才能访问包级变量。如果您必须 import Test,那么我假设它Test在不同的包中,因此它无法访问i.

Test2访问i,请将其定义在与Test1.

于 2013-03-11T20:31:28.213 回答
0

你忘了写

package pkg1; 

对于 Test2 类。

它现在应该可以工作

于 2013-03-11T20:38:56.210 回答