2

Maybe I am using the package concept wrong but I have made a package which has all the classes with common Bluetooth behavior and then sub packages with classes for SPP, HDP, and BTLE. Now what I would like to do is access methods in the classes in the common Bluetooth package in all the sub packages without making it public.

One way to do that is to extend classes but that runs into problems due to my need for Android Contexts and BroadcastReceivers.

I could do what I want if I put all the classes into a single package but that is not quite as nicely organized.

Brian Reinhold
  • 2,313
  • 3
  • 27
  • 46
  • 1
    Packages in Java don't have that concept. You should probably use **modules** for that. – ernest_k May 28 '18 at 09:38
  • 1
    If you want to call methods from out side the package you have to make them public. otherwise you have to use reflection, it is not nice – janith1024 May 28 '18 at 09:42
  • Already answered here: https://stackoverflow.com/questions/1967229/java-subpackage-visibility – Gaurav May 28 '18 at 09:44
  • Yes it is a duplicate. I did not select the correct search parameters to find it....and the answer appears to be as I feared. I am ignorant of the 'modules' concept so that may provide some solution – Brian Reinhold May 28 '18 at 11:30
  • 1
    @gaurav thank you for providing the link. I wish the moderators would follow your example. – Brian Reinhold Apr 07 '19 at 23:09

1 Answers1

4

In Java there is no concept of a subpackage. Even if you create packages inside a package. All of these are separate packages. You won't get any advantage out of it.

Abhishek
  • 688
  • 6
  • 21
  • Not strictly true. Java has subpackages per the JLS, but they do nothing other than add some naming constraints preventing classes from sharing the same name as the subpackage. In particular, as you mention, they don't confer any special access relation between them. — https://stackoverflow.com/a/59956720/1108305 – M. Justin Dec 23 '20 at 07:12