what are the various modifiers available in java?

The access modifiers place a vital role in java programming.
The list of various modifiers available in java is as follows:
  1. public
  2. private
  3. protected
  4. <default>
  5. abstract
  6. final
  7. static
  8. native
  9. synchronized
  10. strictfp
  11. transient
  12. volatile
In java there is no such thing called access specifiers and all these are considered as access modifiers.
The modifiers and there applicability on classes, methods, blocks, variables, interfaces, constructors and enums are given below.

Modifiers and their applicability

Important Conclusions:

  1. The modifiers wihich are applicable for inner classes but not outer classes are private, protected, static.
  2. The modifier that is applicable to class but not interface is final
  3. The modifiers that are applicable to classes but not enums are final and abstract.
  4. The modifier which is applicable to only methods and cannot be used anywhere else is native.
  5. The only modifiers which are applicable to constructors are public,private,protected, default.
  6. The only applicable modifier for local variables is final.
We can declare,
  • a class inside a class
  • a class inside an interface
  • an interface inside an interface 
  • an interface inside a class.
The interface which is declared inside a class is always static implicitly.
The interface which is declared inside an interface is always public and static implicitly.
The class which is declared inside an interface is always public and static implicitly.

Illegal Combinations :

 Methods:
  1. If a method is public, we cannot declare it as private or protected.
  2. If a method is abstract, then we cannot declare it as final, static, private, synchronized, strictfp, native.
Variables:
  1. If a variable is public, we cannot declare it as private or protected.
  2. If a variable is final, we cannot declare it as volatile.
Class:
  1. If a class is public, we cannot declare as private or protected. 
  2. If a class is final, we cannot declare as abstract.
This is applicable to inner classes also .
 

No comments:

Post a Comment