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


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. What value will the following method return?

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

Q7. What can prevent execution of a code in a finally block?
A7. 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 Friendly
The following two tabs change content below.
Arulkumaran Kumaraswamipillai
Mechanical Engineering to Java freelancer since 2003. 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.
Arulkumaran Kumaraswamipillai

Mechanical Engineering to Java freelancer since 2003. 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.

Posted in FAQs Free, Reserved Key Words
Tags: , ,

800+ Interview Q&As with lots of diagrams & code ♥Free | ♦FAQs | Hover/Slide for full text

open all | close all

200+ Java Interview FAQs – Quick Prep

open all | close all

16 Java Key Areas to be a top-notch

open all | close all

80+ Java Tutorials – Step by step

open all | close all

100+ Java Coding Exercises

open all | close all

How good are your

open all | close all