When I try to compile NameTest.java I get an error - cannot access GetSet2
But if I uncomment the first line //package TestPack; in NameTest.java, everything works fine.
Could somebody please explain? Must a class be in a package to access another public class from a different package? But that does not seems to be true because we can import java packages and use the classes in the code.
If you will keep commented the line //package TestPack; it means that you have classes in two differenct packages.In this case all the non public classes defined in that file can not be accessed outside the package.If trying to do so will throw a compilation error.
~Sanjeev Singh<br />SCJP 1.5
posted 11 years ago
Hi Sanjeev, thanks for your reply.
But the two classes are in different packages, TestPack and TestPack2. When I include the NameTest class in a package (not the same as clas GetSet2) and import other package (where class GetSet2 is) it works fine.
When NameTest is not in any package, it can not see GetSet2. I am not able to get why.
The City calls upon her steadfast protectors. Now for a tiny ad: