6 Java Modifiers every interviewer likes

Java modifiers like final, finally, const, volatile, transient and finalize are very popular with the job interviewers. You will see why because of the possible drill down questions.

Q1. What is the difference between ‘final‘ and ‘const‘ modifiers on a variable?
A1. This is a bit of a tricky question because the ‘const‘ is a reserved keyword in Java, but not used. In C++ it means a variable is a constant(i.e. its values cannot be changed).

1. final

The final keyword in Java can be used in many contexts – variable, method, and class. If you make any method as final, you cannot override it. If you make any class as final, you cannot extend it.

Q. Can you inherit a final method in a subclass?
A. Yes. But can’t override it.

Final modifier on a reference variable just means that the reference cannot be changed to reference a different object once assigned. This does not mean that the variable is a constant because the values of the object it refers to can be modified unless the values themselves are marked as final. For example, an Employee object marked as final may have member variables “firstName”, and “lastName”. The values of “firstName”, and “lastName” can be modified if they themselves are not marked as final.

2. const

is a reserved keyword in Java to make a variable constant, so that the referenced object values also cannot be modified, but it is currently not used in Java. The compiler will complain if you use it.

Q2. What is a volatile key word in Java?
A2. The volatile keyword is used with object and primitive variable references to indicate that a variable’s value will be modified by different threads.

3. Volatile

means

  • The value of this variable will never be cached locally within the thread, and all the reads and writes must go to the main memory to be visible to the other threads. In other words the keyword volatile guarantees visibility.
  • From JDK 5 onwards, writing to a volatile variable happens before reading from a volatile variable. In other words, the volatile keyword guarantees ordering, and prevents compiler or JVM from reordering of the code.

Q3. How does  volatile keyword differ from the synchronized keyword?
A3.

  1. The volatile keyword is applied to variables of both primitives and objects, whereas the synchronized keyword is applied to objects, methods, and code blocks.
  2. The volatile keyword only guarantees visibility and ordering, but not atomicity, whereas the synchronized keyword can guarantee both visibility and atomicity if done properly. So, the volatile variable has a limited use, and cannot be used in compound operations like incrementing a variable.

Wrong use of volatile in a compound operation

Right use of volatile. Example1:

Or in lazy singleton. Example2: Double checked locking

Important: Synchronized keyword (i.e. locking) can guarantee both visibility and atomicity, whereas volatile variables can only guarantee visibility. A synchronized block can be used in place of volatile but the inverse is not true.

Thread-safe eager or early loaded singleton:

You can learn more in detail at “10+ Atomicity, Visibility, and Ordering interview Q&As on Java Memory Model (JMM) to understand multi-threading

Q4. Why is the modifier used for thread-safety is called “synchronized”, and NOT “locked”?
A4. When a method or block of code is locked with the modifier “synchronized”, the memory (i.e. heap) where the shared data is kept is synchronized. This means,

JVM memory Vs. physical memory

JVM memory Vs. physical memory

When a synchronized block of code or method is entered after the lock has been acquired by a thread, it first reads (i.e. synchronizes) any changes to the locked object from the main heap memory to ensure that the thread that has the lock has the current info before start executing.

After the synchronized block has completed and the thread is ready to relinquish the lock, all the changes that were made to the object that was locked is written or flushed back (i.e. synchronized) to the main heap memory so that the other threads that acquire the lock next has the current info.

This is why it is called “synchronized” and not “locked”. This is also the reason why the immutable objects are inherently thread-safe and does not require any synchronization. Once created, the immutable objects cannot be modified.

You can learn more in detail at “10+ Java Memory Model (JMM) interview Q&As to understand multi-threading

Q5. What is a “transient” modifier? Can you mark a static variable as transient?
A5.  It marks a member variable not to be serialized when it is persisted to streams of bytes. It cannot be used with a static variable as a static variable belongs to a class, not to an object. You can only serialize an object.

4. Transient

Serialization converts an object state to serial bytes (i.e. flattening an object). Those bytes are sent over the network and the object is recreated from those bytes. Member variables marked by the java transient keyword are not transferred over the wire. A “File” object cannot be serialized.

Non memory objects like sockets, file handles, etc cannot be serialized, hence mark them as “transient”.

Note: @Transient annotation suggests that the object should not be persisted in Hibernate.

Q6. In Java, what purpose does the key words final, finally, and finalize fulfill?
A6. final‘ makes a variable reference not changeable, makes a method not overridable, and makes a class not inheritable.

5. finally

finally‘ is used in a try/catch statement to almost always execute the code. Even when an exception is thrown, the finally block is executed. This is used to close non-memory resources like file handles, sockets, database connections, etc till Java 7. This is is no longer true in Java 7.

Java 7 has introduced the AutoCloseable interface to avoid the unsightly try/catch/finally(within finally try/catch) blocks to close a resource. It also prevents potential resource leaks due to not properly closing a resource.

//Pre Java 7

Java 7 – try can have AutoCloseble types. InputStream and OutputStream classes now implements the Autocloseable interface.

try can now have multiple statements in the parenthesis and each statement should create an object which implements the new java.lang.AutoClosable interface. The AutoClosable interface consists of just one method. void close() throws Exception {}. Each AutoClosable resource created in the try statement will be automatically closed without requiring a finally block. If an exception is thrown in the try block and another Exception is thrown while closing the resource, the first Exception is the one eventually thrown to the caller. Think of the close( ) method as implicitly being called as the last line in the try block. If using Java 7 or later editions, use AutoCloseable statements within the try block for more concise & readable code.

6. finalize

finalize‘ is called when an object is garbage collected. You rarely need to override it. It should not be used to release non-memory resources like file handles, sockets, database connections, etc because Java has only a finite number of these resources and you do not know when the Garbage Collection (i.e. GC) is going to kick in to release these non-memory resources through the finalize( ) method.

[ Further reading: 9 Java Garbage Collection interview Q&As to ascertain your depth of Java knowledge ]

So, final and finally are used very frequently in your Java code, but the key word finalize is hardly or never used.

final

Q7. What value will the following method return?

A7. 1 is returned because ‘finally’ has the right to override any exception/returned value by the try..catch block. It is a bad practice to return from a finally block as it can suppress any exceptions thrown from a try..catch block. For example, the following code will not throw an exception.

Q8. What can prevent execution of a code in a finally block?
A8. a) An end-less loop.

b) System.exit(1) statement.

c) Thread death or turning off the power to CPU.
d) An exception arising in a finally block itself.
e) Process p = Runtime.getRuntime( ).exec(“”);

If using Java 7 or later editions, use AutoCloseable statements within the try block.

print


Why wait 5-10 when you can fast-track in 1-2 ?

open all | close all
Top