Modifiers are used on classes and class members (constructors, methods , data, and class-level blocks), but the final modifier can also be used on local variables in a method. A modifier that can be applied to a class is called a class modifier . A modifier that can be applied to a method is called a method modifier . A modifier that can be applied to a data field is called a data modifier . A modifier that can be applied to a class-level block is called a block modifier . The following table gives a summary of the Java modifiers.
Modifier | class | constructor | method | data | block | Explanation |
---|---|---|---|---|---|---|
(default) [*] | A class, constructor, method, or data field is visible in this package. | |||||
public | A class, constructor, method, or data field is visible to all the programs in any package. | |||||
private | A constructor, method or data field is only visible in this class. | |||||
protected | A constructor, method or data field is visible in this package and in subclasses of this class in any package. | |||||
static | Define a class method, or a class data field or a static initialization block. | |||||
final | A final class cannot be extended. A final method cannot be modified in a subclass. A final data field is a constant. | |||||
abstract | An abstract class must be extended. An abstract method must be implemented in a concrete subclass. | |||||
native | A native method indicates that the method is implemented using a language other than Java. | |||||
synchronized | Only one thread at a time can execute this method. | |||||
strictfp | Use strict floating-point calculations to guarantee that the evaluation result is the same on all JVMs. | |||||
transient | Mark a nonserializable instance data field. |
[*] Default access has no modifier associated with it. For example: class Test {}