♥♦ 6 Java Modifiers every interviewer seems to like

Q1. In Java, what purpose does the key words final, finally, and finalize fulfill?
A1. Covered in detail at Q1-Q10: Top 50+ Core Java Interview Questions & Answers

Q2. What is the difference between ‘final‘ and ‘const‘ modifiers on a variable?
A2. 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).


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.


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.

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



  • 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.

Q4. How does  volatile keyword differ from the synchronized keyword?

  1. The volatile keyword is applied to variables of both primitives and objects, whereas the synchronized keyword is applied to only objects.
  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.

So, if you are not sure where to use, then use the “synchronized” keyword, and stay clear of the volatile modifier. You can learn more in detail at “10+ Atomicity, Visibility, and Ordering interview Q&As on Java Memory Model (JMM) to understand multi-threading

Q5. Why is the modifier used for thread-safety is called “synchronized”, and NOT “locked”?
A5. 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

Q6. What is a “transient” modifier? Can you mark a static variable as transient?
A6.  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.


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.

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.


The following two tabs change content below.
Arulkumaran Kumaraswamipillai
Mechanical Engineer to freelance Java developer within 3 years. Freelancing since 2003 for the major banks, telecoms, retail & government organizations. Attended 150+ Java job interviews, and most often got 3-6 job offers to choose from. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. Join my LinkedIn group. 1,350+ paid memberships.
Tags: ,

By topics – 800+ Q&As ♥ Free ♦ FAQ

open all | close all

Java 200+ FAQs – Quick Brushup

open all | close all

100+ Java Tutorials step by step

open all | close all

13+ Tech Key Areas to standout

open all | close all

Java coding exercises

open all | close all