What does the “private package” mean? [duplicate]

2019-03-09 17:45发布

This question already has an answer here:

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?

标签: java package
6条回答
狗以群分
2楼-- · 2019-03-09 18:09

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

查看更多
神经病院院长
3楼-- · 2019-03-09 18:15

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.

查看更多
4楼-- · 2019-03-09 18:17

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).

查看更多
神经病院院长
5楼-- · 2019-03-09 18:27

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

查看更多
迷人小祖宗
6楼-- · 2019-03-09 18:28

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

查看更多
smile是对你的礼貌
7楼-- · 2019-03-09 18:33

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}

查看更多
登录 后发表回答