|
We will call all the remaining modifiers besides the access modifiers as the other modifiers. These remaining modifiers specify either how a particular construct is stored or how it should be implemented. Implementation-specific modifiers are abstract, final, native, static and synchronized. They specify how you are allowed to implement a particular construct. For example, if you use the final modifier in the declaration of a class, it means that the class cannot be extended. The transient and volatile are the storage-specific modifiers. They are applicable only to member variables and they change the way these variables are stored. For example, the transient modifier applied to a member variable specifies that it cannot be serialized when the class is archived.
Note that you cannot use each and every modifier in the class and its member declarations. For example, you cannot use the modifier abstract in the declaration of member variable. Table 3.2 shows all the combinations of the other modifiers and declarations in which they can be used.
Table 3.2 Applying of simple modifiers to classes, methods, variables and blocks
|
Modifiers |
Top-level Class |
Inner/nested Class |
Member Variable |
Member Method |
Constructor |
Free-floating Code block |
Local Variable |
|
abstract |
ü |
ü |
- |
ü |
- |
- |
- |
|
final |
ü |
ü |
ü |
ü |
- |
- |
ü |
|
static |
- |
ü |
ü |
ü |
- |
ü |
- |
|
native |
- |
- |
- |
ü |
- |
- |
- |
|
synchronized |
- |
- |
- |
ü |
- |
ü |
- |
|
transient |
- |
- |
ü |
- |
- |
- |
- |
|
volatile |
- |
- |
ü |
- |
- |
- |
- |
|
strictfp |
ü |
ü |
- |
ü |
- |
- |
- |
|
|
Note that you can use more-than-one modifier while declaring class or its members. When you do so, the modifiers may appear in any order in that declaration. For example, you can apply the access modifiers either before or after or in between the other modifiers. |