Package
提供管理名字空间的机制。
一个java源代码文件为一个编译单元(compilation unit or translation unit),只能有一个public class,如果有别的class,必须hidden from the public。
Code organization
- Compile a java file -> Get output file for each class (with “.class” extension.)
- A working prgram is a bunch of .class files compressed into a java ARchive file. The java interpreter is responsible for finding loading, and interpreting these files.
- A library is a group of these files.
- package and import keywords allow you to divide up the single global namespace so you won’t have clashing names.
Creating unique package names
By convension, the first part of the package name is the reversed Internet domain name of the creator.
Java access specifiers
Package access
- Use no access specifers, default acess.
- public to other classes in the same package, private to all classes outside.
public: interface access
The default package: if two compilation are in the same directory and havent specified their packages, they are granted package access by default.
private access: you cant touch that
No one can access that member except the class that contains it.
protected access: inheritance access
- Grant access to derived classes
- Give package access
Interface and implementation
Seperate the interface and implementation
Class access
- Only one public class per compilation unit
- The name of the public class must exactly match the name of the file.
- If a compilcation unit has no public class, you can name the file whatever you like.
- class cannot be private or protected
- If a class has private constructor, we can use static method to create new object.