Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Class access question  RSS feed

 
Kiran Gavate
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kiran Gavate
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!