版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/85265566
1. package access class with public constructor
// hiding/packageaccess/PublicConstructor.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package hiding.packageaccess;
class PublicConstructor {
public PublicConstructor() {}
}
2. access public construtor above
// hiding/CreatePackageAccessObject.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {WillNotCompile}
import hiding.packageaccess.*;
public class CreatePackageAccessObject {
public static void main(String[] args) {
new PublicConstructor();// compile error
}
}
my compiler error message:
CreatePackageAccessObject.java:7: error: package hiding.packageaccess does not exist
import hiding.packageaccess.*;
^
CreatePackageAccessObject.java:11: error: cannot access PublicConstructor
new PublicConstructor();
^
bad source file: ./packageaccess/PublicConstructor.java
file does not contain class PublicConstructor
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
2 errors
Summary, declaring a constructor public inside a packageaccess class doesn't actually make it public, and it should probably be flagged with a compiler error at the point of declaration.
references:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/hiding/packageaccess/PublicConstructor.java
3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/hiding/CreatePackageAccessObject.java