Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "private package" mean? [duplicate]

Tags:

java

package

Please see the sample:

private package com.xm.aws;  import static com.xml.aws.PcgTest.test;  public class PackageTest {     public static void main(String[] args) {         test(args);     } } 

What does the private tell me about the package?

like image 410
Sstx Avatar asked Jun 19 '13 08:06

Sstx


2 Answers

Let's not confuse this with package-private or other access modifiers that can be added to classes, methods and fields.

The Java language specification clearly states:

6.6.1. Determining Accessibility

  • A package is always accessible.

Looking at that, the only answer, that comes to my mind is, that (some) compilers don't treat this as a compiletime error but that it is completely meaningless. It is not possible to restrict accessibility to a class or package that way (and every package is always accessible).

Another section from the java language spec:

7.4.1. Named Packages

A package declaration in a compilation unit specifies the name (§6.2) of the package to which the compilation unit belongs.

PackageDeclaration:

Annotationsopt package PackageName ;

So the keyword may be preceeded by annotations. But the access modifiers is not part of the package declaration. And even if we expand on "Annotations" we won't find access modifiers here.

Another reference, according to JLS 18. Syntax the only thing allowed to precede package is an Annotation.

CompilationUnit:
[[Annotations] package QualifiedIdentifier ;]
{ImportDeclaration} {TypeDeclaration}

like image 178
Andreas Dolk Avatar answered Oct 09 '22 05:10

Andreas Dolk


The code sample you have provided is not valid in java. The private access modifier can be applied to members and methods, including inner classes. Your code compiles in Eclipse, but is rejected by Oracle's own compiler.

In fact, the byte-code generated by Eclipse for this java code, is exactly the same with or without that private keyword. This shows that this is probably an Eclipse bug where it ignores the text before the word package during compilation.

What you have probably read or heard, is the phrase "package-private", which means that nothing outside the package can access the class or member. You do this by not using any access modifier on the class itself. Not by using the private keyword on the package.

like image 42
Rajesh J Advani Avatar answered Oct 09 '22 05:10

Rajesh J Advani