Interview Q&A · Java · 4 questions in this topic
Constants & Literals — Java Interview Q&A
Creating constants with final, and the difference between compile-time and runtime constants. Part of Core Java Basics · 294 questions in the Java bank.
A constant is a promise, not a magic number with a nicer name: final fixes the binding, static shares one copy, and a compile-time constant expression is folded into every caller. That folding is why changing a public static final primitive requires recompiling dependent code, and why a runtime-computed final (a new object or a method result) is not inlined. Prefer private constants with accessors when the value may change, and reserve public constants for genuinely stable protocol values such as timeouts and limits.
What are constants and how do you create them in Java?
Answer
A constant is a value that cannot change after it is assigned. In Java you create one with the final keyword; by convention a constant is also static when it belongs to the type rather than to an instance, and its name is written in UPPER_SNAKE_CASE. If the initialiser is a compile-time constant expression, the compiler inlines the value at every use site.
Key points
- 1final means "assigned once"; static final is the conventional shape for a named constant.
- 2A final reference cannot point at a different object, but the object it points at may still be mutable.
- 3Interfaces may declare constants (fields are implicitly public static final) but a class/enum is usually clearer.
- 4Only literals and constant expressions are inlined; a final field built with new or a method call stays a runtime constant.
- 5For a fixed set of related values, an enum usually communicates intent better than a group of int flags.
Example
public static final int MAX_USERS = 100; public static final String APP_NAME = "DSA-topics";
What is the difference between final, finally and finalize?
Answer
They share a name and nothing else. final is a modifier: a final variable is assigned once, a final method cannot be overridden, and a final class cannot be extended. finally introduces a block that runs after a try, whether it completed, threw, or returned. finalize is a protected method on Object that the garbage collector used to call before reclaiming an object; it is deprecated since Java 9, deprecated for removal since Java 18 and disabled by default, so cleanup should use try-with-resources or Cleaner instead.
Key points
- 1final = cannot be reassigned, overridden or extended.
- 2finally = guaranteed execution path for cleanup.
- 3finalize() = unreliable, deprecated object cleanup hook.
- 4Prefer try-with-resources for closing resources deterministically.
Can a final field be assigned outside its declaration?
Answer
Yes, once. A blank final field is declared without an initialiser and must be definitely assigned by the end of every constructor — or, for a static blank final, by the end of the static initialiser. The compiler enforces that exactly one assignment happens on every path, so final does not mean “initialised at the declaration”, it means “assigned exactly once before use”. Assigning it again, even from another constructor path, is a compile error.
Key points
- 1A blank final must be assigned in every constructor.
- 2A static blank final must be assigned in a static initialiser.
- 3Conditional assignment is allowed only if every path assigns once.
- 4final is checked at compile time, not at run time.
Example
class Config { private final int limit; // blank final Config(int limit) { this.limit = limit; } }
When is a constant inlined, and why does that matter?
Answer
A static final field of a primitive or String type whose initialiser is a constant expression is a compile-time constant. javac substitutes its value directly into every use site, so the class file of the caller contains the number itself. If the constant later changes, classes compiled against the old value keep the old number until they are recompiled. A final field computed at run time — with new, a method call or a non-constant expression — is not inlined and is read from the field each time.
Key points
- 1Only constant expressions of primitive or String type are inlined.
- 2Inlining is why public constants can cause stale values across builds.
- 3Recompiling the whole module after changing a constant is mandatory.
- 4Runtime-computed finals avoid the problem but cost a field read.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission