BIG
DATA

JAVA

NON ACCESS MODIFIERS IN JAVA

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

Java provides a set of non access modifiers to produce many other functionality. Non-access modifiers do not change the accessibility of variables and methods, but they do provide them special properties. Non access modifiers can alter the behavior of elements in java. There are two levels on which non access modifiers can be applied, they are

  • classes

    On a class level, there are two non access modifiers:

    • final
    • abstract
  • members

    Members have a lot of non access modifiers but let's look at a few of them:

    • final
    • abstract
    • static
    • synchronized
    • native
    • transient
    • volatile
    • strictfp

Now lets dig into these non access modifiers briefly..

final

final Classes

When a class is set to final, it cannot be extended by any other class. It is a final segment of class hierarchy of the program.

final class Car{ 
	//some code
}
final methods

A method when set to final cannot be overridden by any child class. The final modifier prevents a method from being modified in a childclass.

class Car{ 
	public final void displaySpeed(){
		// body of method
	}
}
final variables

When a variable is set to final, its value cannot be changed. Final variables are like constants. A reference variable declared final can never be reassigned to refer to an different object. However the data within the object can be changed. So the state of the object can be changed but not the reference. A final variable can be explicitly initialized only once.

Most oftenly with variables, final is used along with static to make the constant a class variable.

class Car{ 
	public static final int SPEED = 60;
	public final void displaySpeed(){
		// body of method
	}
}

abstract

abstract Classes

If a class is declared as abstract, then that class can never be instantiated. The sole purpose for using abstract is for the class to bne extended.

Note:

  • A class cannot be declared both abstract and final
  • If a class contains one or more abstract methods then the class should be declared abstract, or else a compile error will be thrown.
  • An abstract class may contain both abstract methods as well normal methods.
  • An abstract class does not need to contain abstract methods.
abstract methods

Abstract methods are those methods which does not have a body but only a signature. The method body is provided by sub class. Abstract methods can never be final. Any class that extends an abstract class must implement all the abstract methods of the super class unless the subclass is also an abstract class.

Note:The abstract method ends with a semicolon.

public abstract class SuperCar{
    abstract void displaySpeed(); //abstract method
}

static

static variables

If you declare a variable as static then it becomes static variable. Static variables are also called as Class variables as it belongs to the class rather than objects(instances).

static methods

Static methods are conceptually the same as static variables. It is a method which belongs to the class and not to the object(instance). A static method can access only static data. It can not access non-static data (instance variables)

We will discuss all about static in detail in static section later.

synchronized

When a method is synchronized it can be accessed by only one thread at a time. This can only be used to methods. The synchronized modifier can be applied with any of the four access level modifiers. We will discuss it in detail in Threads section later.

public synchronized void displaySpeed( ) {   }

native

The native keyword is applied to a method to indicate that the method is implemented in native code using JNI(Java Native Interface). It marks a method, that it will be implemented in other languages, not in Java.

Native methods were used in the past to write performance critical sections but with Java getting faster this is now less common.

Native methods are currently needed when

  • You need to call a library from Java that is written in other language.
  • You need to access system or hardware resources that are only reachable from the other language

transient

transient is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes. Variables may be marked transient to indicate that they are not part of the persistent state of an object.

We will discuss transient in detail in serialization section later.

public transient int speed = 60; //transient value

volatile

Declaring a volatile Java variable means, the value of this variable will never be cached thread-locally, all reads and writes will go straight to "main memory"

We will discuss volatile in detail in threads section later.

public volatile int speed = 60; //volatile value

strictfp

strictfp is a keyword in the Java programming language that restricts floating-point calculations to ensure portability. The strictfp command was introduced into Java with the Java virtual machine (JVM) version 1.2 and is available for use on all currently updated Java VMs.

Strictfp ensures that you get exactly the same results from your floating point calculations on every platform. If you don't use strictfp, the JVM implementation is free to use extra precision where available.

strictfp can be used on classes, interfaces and non-abstract methods. When applied to a method, it causes all calculations inside the method to use strict floating-point math. When applied to a class, all calculations inside the class use strict floating-point math.

public strictfp class MyFPclass { 
    // ... contents of class here ...
}