Non Access Modifiers in Java III: abstract, transient, volatile, native

This lesson deals with abstract, transient, volatile, native non access modifiers. We have already seen static and strictfpsynchronized and final non access modifiers.

The abstract Non Access Modifer

The abstract qualifier is out and out object oriented. It can be applied to

  1. Methods
  2. Classes

abstract Methods

  1. A method is declared abstract by saying so in the method definition and declaration.
  2. An abstract method is an empty method. Definition of method is delegated to sub classes. There are exactly zero statements in an abstract method. (A non abstract method can also have zero statements in it).
  3. An abstract method can never be final. abstract methods are intended to be overridden by sub classes.
  4. Any class containing an abstract method must itself be abstract.
  5. Any subclass of a class containing abstract method must either itself be abstract or must override the abstract method.
  6. All methods declared in an interface are abstract.

abstract Classes

  1. A class is declared abstract by using the abstract keyword in class definition.
  2. An abstract class may  contain zero or more abstract methods. It may also contain zero or more concrete methods.
  3. A class having one or more abstract methods must be declared abstract.
  4. An abstract class cannot be instantiated.
  5. An abstract class cannot be final.
  6. An abstract class provides a template for the more specific child classes.

The native Non Access Modifer

  1. The native non access modifier can be applied only to methods.
  2. Can be applied to both static and instance methods.
  3. Tells the compiler and the JVM that the implementation of the method will not be found in Java. Meaning, JVM’s method area will not contain code describing this method.
  4. Indicates that the code has been written in an external language. All of the native code is written in C and C++.
  5. Involves the following steps –
    1. Generating the header files
    2. Writing the implementation of the native method in C/C++
    3. Using your C/C++ compiler to create a shared library
    4. Run the program using java interpreter

The volatile Non Access Modifer

  1. Applies only to instance variables. Cannot be applied to other constructs such as local variables or methods or classes.
  2. Tells the Java thread not to rely on the value currently visible to it.
  3. A volatile variable may change value in main memory while the thread continues to see the value it’s PC register. If you need a refresher, see this previous lesson – Inside JVM.
  4. Therefore, upon every access of a volatile variable, the runtime fetches the latest value from memory.
  5. Reads and writes to a volatile variables are atomic. This point will be discussed further in concurrency lessons, but know that Java data types such as long (64 bits) may map to more than one word of underlying architecture.
  6. By virtue of above fact, volatile variables can be used in multithreaded programs
  7. Declaring a member to be volatile is cheapest way of writing lock-free multithreaded code
  8. A volatile primitive data type such as float and char stores the value directly.
  9. A volatile reference data type stores the reference to an object. Therefore, the underlying object may  or may not change and has got no direct bearing on declaring the reference volatile. It is far less idiomatic to write Java programs that work by continually assigning new objects to reference.
  10. Due to points #8 and #9, volatile variables are far more useful and common with primitive data types.

The transient Non Access Modifer

The transient modifier –

  1. Applies only to instance variables
  2. Tells the JVM to not include the field in serialization (and therefore de-serialization as well)
  3. Is generally used when an instance variable of an object is a function of factors outside object. For example, you may have a boolean field called isMoving inside the vehicle. Whether a vehicle is moving or not depends on the external traffic light and traffic conditions. Therefore, such variables should be declared transient.

Leave a comment

Your email address will not be published.