Forums Register Login

Class access question

+Pie Number of slices to send: Send
Hi,

I have following code in the file NameTest.java


and this code in GetSet2.java



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.

Kiran
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

Kiran
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 561 times.
Similar Threads
Package Issue / Class Path issue
Crazy protected modifier: K&B book lies?
package does'nt exist compiler error
accessing private variables of thread class using reflection
Private member access
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:40:14.