• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class access question

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic