27

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?

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
Sstx
  • 604
  • 1
  • 8
  • 21

6 Answers6

24

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}

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • 1
    Finally. An answer ;) – Maroun Jun 19 '13 at 08:37
  • Interesting though, that annotations may be applied at package level - although i cant think of a good use case for that..? – Gyro Gearless Jun 19 '13 at 08:47
  • 2
    @GyroGearless In the end it's a bug in eclipse. Access modifiers are not part of the package declarations and so they're somewhat "isolated" at the beginning at the source file. They don't modify anything here. eclipse needs to be fixed. – Andreas Dolk Jun 19 '13 at 08:49
  • I clearly agree this is an eclipse bug. I was referring to the JLS section 7.4.1 you quoted, which allows annotations (not access modifiers) at package level. – Gyro Gearless Jun 19 '13 at 08:56
  • 3
    http://stackoverflow.com/questions/2099431/whats-the-point-of-package-annotations – Tim Bender Jun 19 '13 at 08:59
  • I think this is **not** an Eclipse bug. The answer shows it is allowed according to Java language specifications and therefore valid. It simply doesn't do anything in Java, and is used for other things as the SO question linked by Tim Bender specifies. – ADTC Jun 19 '13 at 09:10
  • @ADTC "The answer shows it is allowed according to Java language specifications" <-- where did you see that? That is quite the opposite: `[[Annotations] package QualifiedIdentifier ;]` <-- this is what a package declaration is like – fge Jun 19 '13 at 09:16
  • @ADTC You are confusing access modifiers with annotations. They are completely different. – FThompson Jun 19 '13 at 09:17
  • You're right, they are called _[ClassModifier](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.1)_ in Java, where _Annotation_ is just a subset. But still, why does it compile and run in Eclipse? Does it compile when using javac? Perhaps the compiler simply ignores the access modifiers instead of complaining about compliance? – ADTC Jun 19 '13 at 09:34
  • @ADTC this is a bug in ECJ (Eclipse Compiler for Java), which Eclipse uses in preference to the JDK. It should be reported to them, if it hasn't been already. – fge Jun 19 '13 at 09:49
  • Reported as [bug 411103](https://bugs.eclipse.org/bugs/show_bug.cgi?id=411103). Apparently this is supposed to be fixed already according to [bug 384567](https://bugs.eclipse.org/bugs/show_bug.cgi?id=384567) but I don't know what happened after that... – ADTC Jun 19 '13 at 10:24
6

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.

FThompson
  • 28,352
  • 13
  • 60
  • 93
Rajesh J Advani
  • 5,585
  • 2
  • 23
  • 35
2

Though package is not the highest degree of Encapsulation in Java which is achieved using private keyword, it still second best option and must to encapsulate whole functionality rather than just a class.

In short, Access modifiers are not part of the package declarations

Refer this link

bNd
  • 7,512
  • 7
  • 39
  • 72
2

If you add private before the package name this will be compiler error

Manoj
  • 311
  • 2
  • 5
  • 14
  • However, it compiles in Eclipse. – FThompson Jun 19 '13 at 08:35
  • 3
    @Vulcan eclipse is not necessary Java. I found errors in the eclipse compiler before therefore this is not a reference. The reference is either Oracle JDK or OpenJDK. – Uwe Plonus Jun 19 '13 at 08:36
  • 2
    @UwePlonus I do not disagree with that. I simply noted that it compiles in Eclipse, which is the reason the question most likely came up to begin with. – FThompson Jun 19 '13 at 08:37
1

Looks to me like it is only happening in eclipse. When i compile the code though javac command through command prompt, i get this compile time error:

error: class, interface, or enum expected

Looking at the post here, looks like eclipse uses its own jdk:

Do I need to install java sdk if I have eclipse

Community
  • 1
  • 1
ajay.patel
  • 1,957
  • 12
  • 15
-1

Writing "private package" and "package" is the same. They identify the same access level (the dafault one).

The private modifier specifies that the member can only be accessed within its own package (as with protected).

Matteo N.
  • 297
  • 2
  • 6