transient and instanceof keywords in Java With Examples

The transient Modifiers

These transient modifiers are used to handle somewhat specialized situations. When an instance variable is declared as transient, its value need not persist when an object is stored. Whenever the JVM finds any variable of type transient, the current value is ignored, and the default value of such variable is saved.

Example:

Class TransientTest{
transient int alpha;
int beta;
}

Here, if an object of type TransientTest is written to a persistent storage area, the current value of alpha would not be saved, but the value of beta will be saved.

Use of transient variables

Privacy

The transient keyword plays a crucial role to meet security constraints. Few data need not be saved at all which are private in nature.  

Non-Serialization

We serialize only those data members which represent the state of an instance, and only such details of the instance need to be saved. All the other members should be used with modifiers transient. The transient keyword is used with a variable which is not to serialized and whose value can be deduced using other serialized data/object.

Example:

Class Employee implements serializable{
private String login;
private transient String password;  // The password is transient for security purpose
private Date dateJoined;
private transient int experience;  // The experience  is transient as it can be deduced using dateJoined.
}

 

The instanceof Keyword

The use of instanceof is to know whether an object is an instance of any specific type(class/subclass)  at the run time. Hence it is a run-time operator.

Declaration: X instanceof Y

Here X is the reference to an instance and Y is the class type. The instanceof returns in boolean. It returns to true if the evaluation is correct i.e X is actually an instance of Y, otherwise false. Even though it might look very trivial, but there are several uses of instance keyword.

Object-specific decision

There might be a situation where a number of objects of different types are created. Each object is packed together in some collection e.g ArrayList, array. Despite packed together, it is important to decide the approach on the basis of the object type. Hence, instanceof is used.

Casting

We are aware of upcasting and downcasting. It is also known that the parent class can keep a reference to their child's classes. However, we cannot cast the child classes to one another. So it gets important to detect what reference the variable is actually holding at the instant in the run time.  Let us take an example:

We have class A and B. Class A is inherited by both class C and D.

class A{
int alpha;
int beta;
}

class B{
int alpha;
int beta;
}

class C extends A{
int gamma;
}

class D extends A{
int gamma;
}

public class ClassMain{
public static void main(String args[]){
A a = new A();
B b = new B();
C c = new C();
D d = new D();

if(a instanceof A)
System.out.println("a is instance of A");

if(b instanceof B)
System.out.println("b is instance of B");

if(c instanceof C)
System.out.println("c is instance of C");

if(c instanceof A)
System.out.println("c is instance of A");

if(a instanceof C)
System.out.println("a can be cast to C");
else 
System.out.println("a cannot be cast to C");

System.out.println();

a=d; //A reference to d
System.out.println("a is now referring to d");

if(a instanceof D)
System.out.println("a is instance of D");

System.out.println();

a=c; //A reference to c
System.out.println("a is now referring to c");

if(a instanceof D)
System.out.println("a can be cast to D");
else
System.out.println("a cannot be cast to D");


if(a instanceof A)
System.out.println("a can be cast to A");
else
System.out.println("a cannot be cast to A");
}
}

Output:

a is instance of A
b is instance of B
c is instance of C
c is instance of A
a cannot be cast to C

a is now referring to d
a is instance of D

a is now referring to c
a cannot be cast to D
a can be cast to A

Here, we can see that the evaluation of each object being an instance of the class returns true. c is an instance A(parent class)  however the vice-versa is incorrect. Since a is holding the reference to c(child class reference) but it cannot be cast to D.

This was all about transient and instanceof . We will discuss assert and strictfp keywords in the next lecture.