Java Modifiers 大体可以分为两类:
1) Access modifiers : public, protected, default, private
2) Non- Access modifiers : strictfp, final and abstract
Below are the modifiers for key word class:
1.public: The class can be used anywhere
2.abstract: The class is an abstract class and no instance of it can be generated
3.final: The class cannot be inherited
4.strictfp(strict floating point): Make the floating-point arithmetic in the class evaluated strictly. This ensures the interoperability of the floating-point calculation.
The following table shows what Access Modifiers are appropriate for classes, nested classes, member variables, and methods:
public
protected
private
visible from anywhere | same as its class | same as its class | visible from anywhere | visible from anywhere |
N/A | its class and its subclass | its class and its subclass, and from its package | N/A | N/A |
only from its package | only from its package | only from its package | only from its package | N/A, default is public |
N/A | only from its class | only from its class | N/A | N/A |
If a class has public visibility, the class can be referenced by anywhere in the program. If a class has package visibility, the class can be referenced only in the package where the class is defined. If a class has private visibility, (it can happen only if the class is defined nested in an other class) the class can be accessed only in the outer class.
If a variable is defined in a public class and it has public visibility, the variable can be reference anywhere in the application through the class it is defined in. If a variable has package visibility, the variable can be referenced only in the same package through the class it is defined in. If a variable has private visibility, the variable can be accessed only in the class it is defined in.
If a method is defined in a public class and it has public visibility, the method can be called anywhere in the application through the class it is defined in. If a method has package visibility, the method can be called only in the same package through the class it is defined in. If a method has private visibility, the method can be called only in the class it is defined in.