Because method and variable members are usually given access control in exactly the same way. Whereas a class can use just two of the four access control levels, members can use all eight: Final, Static, Abstract, Native, Volatile, Transient, Strictfp, Synchronized.
Final
Definition : Final is a constant that means don't modify (or) changed.Rules :
- Final keyword can be applied variables, methods and classes.
- Final class can't be extended by any other class.
- If a final class extends you get a compiler time error.
- Final methods are the methods.
- Final methods can't be overridden.
- This feature is provided to maintain security.
- Final variables are the variables.
- Final variables can't be changed.
Static
Definition : The static is used to create variables and methods that will exist independently of any instances created for the class. All static members exist before you ever make a new instance of a class. There will be only one copy of a static member regardless of the number of instances of that class. All instances of a given class share the same value for any given static variable.Rules :
- Static can be applied methods and variables.
- Static can be applied to inner class.
- Static can be applied to initialization blocks.
- Static can't be applied constructor.
- Static can't be applied class and interfaces.
- Static can't be applied local variables, inner class methods and instance variables.
- Static can't be applied method local inner classes.
- Static methods can also be synchronized.
- Static method can access only static variables.
- Static methods can't abstract.
- They can't refer to this (or) super in any way.
Abstract
Abstract Method : An abstract method is a method that is been declared but not implemented.Abstract Class : An abstract class is a class that is declared abstract keyword. It may (or) may not include abstract methods. An abstract class cannot be instantiated, but they can be sub classed.
Rules :
- An abstract class is a class that contains zero (or) more abstract methods.
- An abstract class can contain instance variables and concrete methods in addition to abstract methods.
- An abstract class and the abstract methods should be declared by using the keyword ‘abstract'.
- All the abstract methods of the abstract class should be implemented in its sub classes.
- We can't create an object to abstract class. But, we can create an object to abstract class.
- The reference of abstract class can be used to refer to objects of its sub classes.
- The reference of abstract class can't refer to individual methods of its sub classes.
- It is possible to derive an abstract class as a sub class from a concrete super class.
- We can't declare a class as both abstract and final.
- An abstract method is a method without method body.
Native
Definition : The Native method indicates that a method is implemented in platform dependent.Rules :
- Native applies to only methods.
- Native can be applied to static methods also.
- Native methods cannot be abstract.
- Native methods can throw exception.
- Native method is like an abstract method.
Volatile
Definition : The volatile modifier tells the JVM that a thread accessing the variable must always reconcile its own private copy of the variable with the master copy in memory. (Or) Volatile is a keyword indicates that a thread accessing the variable must always reconcile its own private copy of the variable with the master copy in memory.Rules :
- Volatile applied variables only.
- Volatile can be applied to static variables.
- Volatile can't be applied to final variables.
- Volatile is used in multi-processor environment.
- Volatile and transient can't come together.
Trainsent
Definition : If you mark an instance variable as transient, you are telling the JVM to skip this variable when you attempt to serialize the object containing it. (Or) Transient is a keyword indicates that a variable cannot be serialization.Rules :
- Transient can be applied only to class level variable (instance variable).
- Local variables can't be declared as transient.
- Object transient variables aren't serialized.
- Transient variables may not be final (or) static.
- But, the complies allows the declaration and no complier time errors is generated.
Strictfp
Definition : strictfp is a keyword that restricts floating-point calculations to ensure portability. This was introduced into Java with the JVM version 1.2.Rules :
- strictfp can be applied on methods.
- strictfp can be applied on classes.
- strictfp can be applied on interfaces.
- strictfp can be applied on abstract methods.
- strictfp can be applied on variables or constructors.
Synchronized
Definition : The synchronized keyword indicates that a method can be accessed by only one thread at a time.Rules :
- Synchronized keyword can be applied methods only.
- Synchronized keyword cannot be applied variables and classes.
- Each an object has only one lock.
- All the methods in a class must not be synchronized.
- Static methods can also be synchronized.
- Multiple threads can have access to non-synchronized methods.
- If two methods are synchronized then only one thread can access any of the two methods.
Can be applied to Class, Methods, Instance Variables, Static Variables, Local Variables
Non Access Modifiers | Class | Method | Variables |
---|---|---|---|
Final | Yes | Yes | Yes |
Static | No | Yes | Yes |
Abstract | Yes | Yes | No |
Native | No | Yes | No |
Volatile | No | No | Yes |
Tranisent | No | No | Yes |
Strictfp | Yes | Yes | No |
Synchronized | No | Yes | No |