Interview Q&A · Java · 4 questions in this topic

Constants & Literals — Java Interview Q&A

All Java topics

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

  1. 1final means "assigned once"; static final is the conventional shape for a named constant.
  2. 2A final reference cannot point at a different object, but the object it points at may still be mutable.
  3. 3Interfaces may declare constants (fields are implicitly public static final) but a class/enum is usually clearer.
  4. 4Only literals and constant expressions are inlined; a final field built with new or a method call stays a runtime constant.
  5. 5For a fixed set of related values, an enum usually communicates intent better than a group of int flags.

Example

Example.javajava
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

  1. 1final = cannot be reassigned, overridden or extended.
  2. 2finally = guaranteed execution path for cleanup.
  3. 3finalize() = unreliable, deprecated object cleanup hook.
  4. 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

  1. 1A blank final must be assigned in every constructor.
  2. 2A static blank final must be assigned in a static initialiser.
  3. 3Conditional assignment is allowed only if every path assigns once.
  4. 4final is checked at compile time, not at run time.

Example

Example.javajava
class Config {
    private final int limit;   // blank final
    Config(int limit) { this.limit = limit; }
}

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits