Pages

Non Access Modifiers


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 :
  1. Final keyword can be applied variables, methods and classes.
  2. Final class can't be extended by any other class.
  3. If a final class extends you get a compiler time error.
  4. Final methods are the methods.
  5. Final methods can't be overridden.
  6. This feature is provided to maintain security.
  7. Final variables are the variables.
  8. 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 :
  1. Static can be applied methods and variables.
  2. Static can be applied to inner class.
  3. Static can be applied to initialization blocks.
  4. Static can't be applied constructor.
  5. Static can't be applied class and interfaces.
  6. Static can't be applied local variables, inner class methods and instance variables.
  7. Static can't be applied method local inner classes.
  8. Static methods can also be synchronized.
  9. Static method can access only static variables.
  10. Static methods can't abstract.
  11. 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 :
  1. An abstract class is a class that contains zero (or) more abstract methods.
  2. An abstract class can contain instance variables and concrete methods in addition to abstract methods.
  3. An abstract class and the abstract methods should be declared by using the keyword ‘abstract'.
  4. All the abstract methods of the abstract class should be implemented in its sub classes.
  5. We can't create an object to abstract class. But, we can create an object to abstract class.
  6. The reference of abstract class can be used to refer to objects of its sub classes.
  7. The reference of abstract class can't refer to individual methods of its sub classes.
  8. It is possible to derive an abstract class as a sub class from a concrete super class.
  9. We can't declare a class as both abstract and final.
  10. An abstract method is a method without method body.

Native

Definition : The Native method indicates that a method is implemented in platform dependent.
Rules :
  1. Native applies to only methods.
  2. Native can be applied to static methods also.
  3. Native methods cannot be abstract.
  4. Native methods can throw exception.
  5. 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 :
  1. Volatile applied variables only.
  2. Volatile can be applied to static variables.
  3. Volatile can't be applied to final variables.
  4. Volatile is used in multi-processor environment.
  5. 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 :
  1. Transient can be applied only to class level variable (instance variable).
  2. Local variables can't be declared as transient.
  3. Object transient variables aren't serialized.
  4. Transient variables may not be final (or) static.
  5. 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 :
  1. strictfp can be applied on methods.
  2. strictfp can be applied on classes.
  3. strictfp can be applied on interfaces.
  4. strictfp can be applied on abstract methods.
  5. 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 :
  1. Synchronized keyword can be applied methods only.
  2. Synchronized keyword cannot be applied variables and classes.
  3. Each an object has only one lock.
  4. All the methods in a class must not be synchronized.
  5. Static methods can also be synchronized.
  6. Multiple threads can have access to non-synchronized methods.
  7. 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