package Dir.SubDir;
public class ExampleClass {
...
}
package AnotherDir;
import Dir.SubDir.ExampleClass;
public class AnotherClass {
ExampleClass ec=new ExampleClass();
...
}
import Dir.SubDir.*;
|
DirA/ClassA.java
package DirA;
public class ClassA {
int n=10;
}
|
DirB/ClassB.java
package DirB;
import DirA.*;
public class ClassB {
ClassA a;
public void printA() {
System.out.println("The value of a.n is "+a.n);
}
}
|
DirB/ClassB.java:9: n is not public in DirA.ClassA; cannot be accessed from outside package
System.out.println("The value of a.n is "+a.n);
^
1 error
The solution should be evident from the error message. You need to make the variable "n" public.
So the following will compile correctly.
|
DirA/ClassA.java
package DirA;
public class ClassA {
public int n=10;
}
|
DirB/ClassB.java
package DirB;
import DirA.*;
public class ClassB {
ClassA a;
public void printA() {
System.out.println("The value of a.n is "+a.n);
}
}
|