Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
Chaturmind
← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
HomeLearnJava Interview PrepJava Interview Prep: Fresher to 2 YearsStrings, Keywords, Exceptions & Serialization
✓ FreeBeginner· 7 min read

static & final Keywords — Interview Questions

static fields, methods and blocks, whether static blocks can throw, why static methods can't be overridden or use instance members, and every use of final — including its real impact on thread safety and performance.

Published September 25, 2026


How to use this lesson

static and final look simple, but interviewers use them to test your understanding of class loading, compile-time binding and the memory model. Keep each answer crisp, and add one line of code.

Q1. What does the static keyword mean?

Short answer: static marks a member as belonging to the class rather than to any object. There's one copy, shared by all instances, and it's accessible without creating an object: Math.max(a, b), Integer.MAX_VALUE.

Key points to cover:

  • It can be applied to:
    • Fields: shared state, such as a counter or a constant.
    • Methods: utilities and factories.
    • Blocks: one-time class initialisation.
    • Nested classes: a nested class that doesn't need an outer instance.
  • Static fields live with the class metadata, and exist as long as the class is loaded.
class Ticket {
    private static int issued = 0;          // one counter shared by all tickets
    private final int number;
    Ticket() { number = ++issued; }         // not thread-safe: use AtomicInteger if tickets are created concurrently
    static int issuedCount() { return issued; }
}

Learn it in depth → Classes and Objects

Q2. What is a static block?

Short answer: A static { … } block runs once, when the class is initialised (on first use), before any object is created or any static method runs. It's used for complex static initialisation.

class CountryCodes {
    static final Map<String, String> BY_ISO;
    static {
        Map<String, String> m = new HashMap<>();
        m.put("IN", "India");
        m.put("US", "United States");
        BY_ISO = Collections.unmodifiableMap(m);
    }
}

Key points to cover:

  • Multiple static blocks run in the order they appear in the source.
  • For simple cases, static final Map<String, String> BY_ISO = Map.of("IN", "India", …); is cleaner.

Q3. Can a static block throw an exception?

Short answer: It can't throw a checked exception. There's nowhere to declare a throws clause, so checked exceptions must be caught inside the block. An unchecked exception thrown there is wrapped in an ExceptionInInitializerError, and the class becomes unusable: later attempts to use it fail with NoClassDefFoundError.

static {
    try {
        CONFIG = loadConfig();              // throws IOException
    } catch (IOException e) {
        throw new IllegalStateException("cannot load config", e);   // becomes ExceptionInInitializerError
    }
}

Common trap: saying the exception can be "declared with a throws clause in the class". Classes have no throws clause. This is a common error in prepared answer sheets.

Q4. Can we override static methods?

Short answer: No. Overriding relies on runtime dispatch on the object's type, and static methods are bound at compile time to the class. A subclass static method with the same signature hides the parent's method.

class Parent { static String id() { return "parent"; } }
class Child extends Parent { static String id() { return "child"; } }   // hiding, not overriding

Parent p = new Child();
System.out.println(p.id());   // "parent": decided by the reference type (and a compiler warning)

Q5. Can a static method access non-static members?

Short answer: Not directly. A static method has no this, because no particular object is involved, so it can't refer to instance fields or methods. It needs an object reference to reach them.

class Counter {
    int value;
    static void reset(Counter c) {
        // value = 0;        // compile error: non-static field referenced from a static context
        c.value = 0;         // fine: goes through an instance
    }
}

Key points to cover:

  • The reverse is fine: instance methods can use static members freely.

Q6. What does the final keyword do?

Short answer: It means "can't be changed", in three places:

  • A final variable can be assigned only once.
  • A final method can't be overridden.
  • A final class can't be extended (String, Integer, LocalDate).
final int maxRetries = 3;                // maxRetries = 4; → compile error
class Base { final void audit() { } }    // subclasses can't override audit()
final class Money { }                    // class X extends Money → compile error

Key points to cover:

  • final fields must be assigned exactly once: at their declaration, in an initialiser block, or in every constructor. This is called a "blank final".
  • A static final field with a primitive or String value set from a compile-time constant is a constant. The compiler inlines it into the code that uses it.

Q7. What are common uses of final variables?

Short answer:

  • Constants: static final int MAX_SIZE = 100;.
  • Immutable fields in value objects and injected dependencies.
  • Local variables captured by lambdas or anonymous classes, which must be final or effectively final.
  • Parameters, as documentation that they're never reassigned.
@Service
class OrderService {
    private final OrderRepository repository;         // injected once, never reassigned
    OrderService(OrderRepository repository) { this.repository = repository; }
}

Q8. How does final contribute to immutability and thread safety?

Short answer: final fields can't be reassigned, and the Java Memory Model gives them a special guarantee. Once a constructor finishes, any thread that sees the object also sees the final fields' fully initialised values, without synchronisation (provided this didn't escape during construction).

Key points to cover:

  • final doesn't make the referenced object immutable. A final List can still have items added to it. Combine final with immutable types, or with defensive copies.
  • Non-final fields of an object published without synchronisation can be seen in a half-initialised state by other threads.

Learn it in depth → Volatile and the Java Memory Model

Q9. Are there performance considerations when using final?

Short answer: Very few. The JIT compiler is already good at inlining non-final methods (it tracks which classes are actually loaded and deoptimises if that changes), so marking methods final rarely makes code faster. Use final for design and correctness, not speed.

Key points to cover:

  • Where final does matter:
    • static final compile-time constants are inlined into calling code. Changing one requires recompiling the classes that use it, which is a classic stale-constant bug.
    • final fields get memory-model visibility guarantees.
    • static final fields can be constant-folded by the JIT.

Q10. Does final improve performance by reducing method-call overhead?

Short answer: In early JVMs, somewhat. On modern HotSpot, not meaningfully. The JIT uses class hierarchy analysis to inline methods that have only one loaded implementation, whether or not they're final, and it inlines polymorphic call sites that are hot. Don't claim final as a performance optimisation. Say it communicates intent and prevents incorrect overriding.

Follow-up questions this topic invites — and their answers

Q: What is "effectively final"? A: A local variable that is never reassigned after initialisation, even without the final keyword. Lambdas and anonymous classes can capture it. The rule exists because captured variables are copied, so allowing changes would create confusing, inconsistent state.

Q: Can a constructor be static or final? A: No. Constructors initialise instances, so static makes no sense. They aren't inherited, so final has nothing to prevent.

Q: Can an abstract method be final or static? A: No. An abstract method must be overridden, while final forbids overriding and static methods can't be overridden. Both combinations are compile errors.

Q: What's the difference between final, finally and finalize? A: final is a modifier (no reassignment, no overriding, no subclassing). finally is the block that always runs after try/catch. finalize() is the deprecated GC cleanup hook on Object.

Q: When does a class's static initialisation run? A: On its first active use: creating an instance, calling a static method, or reading or writing a non-constant static field. Merely declaring a variable of that type, or accessing a compile-time constant, doesn't trigger it.

Previous

Strings, String Pool, StringBuilder & StringBuffer — Interview Questions

Next

Exceptions, Generics, Enums & Reflection — Interview Questions

AI Tutor

Lesson: static & final Keywords — Interview Questions

Quick actions

AI responses can be inaccurate. Verify critical information.