1

I have a project P1 that has a package A.B.C within which there is a class MyClass1. This class has default scope. Because of this, it is not visible in any of the other packages within project P1 and certainly not visible to any other project.

However, if I create another project P2 that has a package with the same name A.B.C, I can access the class MyClass1. This sounds bad because any one who wants to use the unexposed API can just create a package with a same name and get the access.

Is this the expected behavior?

--

Another related question: Is there anything in Java for project level scope?

user673218
  • 411
  • 2
  • 8
  • 20
  • Why do you want to do this? –  Nov 22 '13 at 14:03
  • @LutzHorn In my project, one package has few files that are exposed as a part of API. To manage the number of files, I tried moving some of the files to another package. But that requires those classes (previously unexposed) to be public which dilutes my API. I am using Eclipse Juno – user673218 Nov 22 '13 at 14:08

1 Answers1

6

Is this the expected behavior?

Yes. That's why you cannot name your package with java.*.

Is there anything in Java for project level scope?

No.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • `Yes. That's why you cannot name your package with java.*` But I am able to create a package A.B.C in project P2 and access MyClass. Any way, I can restrict that? – user673218 Nov 22 '13 at 14:11
  • 1
    @user673218 I don't think so. Only `java` package is restricted. – Eng.Fouad Nov 22 '13 at 15:43