Nested types: interview questions & scenarios
These are the nested-class questions I hear in Java interviews, from "static nested versus inner" in a first screen to heap-dump scenarios in senior rounds. The syntax takes five minutes to learn; what interviewers really test is whether you know what javac generates behind your back, because that hidden code is where the leaks and surprises come from.
Here's how to use this page: read the question, say your answer out loud first, then open the model answer. The "What if..." follow-ups are the ones interviewers actually ask next. If a question stumps you, the lesson for this topic is Inner, static nested and anonymous classes, with lambdas covered in Functional interfaces in Java.
What's the difference between a static nested class and an inner class?
Tests enclosing instance, this$0, instantiation
Show answerHide answer
Model answer
A static nested class is an ordinary class that lives in the outer class's namespace. It can see the outer class's private members, but it has no link to an outer instance, so you create it with new Order.Builder() and it can only reach instance fields through an explicit reference.
An inner class (non-static member) belongs to one outer instance. javac adds a hidden final Outer this$0 field and passes the outer object through the constructor, so you create it with outer.new Inner(), and it reads outer fields directly. That hidden reference keeps the outer object alive as long as the inner one is.
How I’d say it in the interview
"A static nested class is a normal class scoped inside another; no outer instance. An inner class is tied to one outer object through a hidden this$0 field, so it can read its fields, but it also keeps it alive. I default to static unless I need that link."
Signals depth
Naming this$0, the outer.new Inner() syntax, and Effective Java's "favor static member classes".
Red flags
"Static nested classes can only have static members" or "inner classes can't access private fields".
Follow-ups interviewers ask next
What if a static nested class needs an outer field?
Pass the outer object (or only the field's value) in explicitly. It can then read even
privatefields through that reference, because they're in the same nest.
Why must local variables used in an anonymous class or lambda be effectively final?
Tests variable capture, synthetic fields, stack vs heap
Show answerHide answer
Model answer
Because the class or lambda gets a copy of the value, not the variable. The local lives in the method's stack frame, which is gone when the method returns, while the object may live on (stored, returned, run on another thread). So javac copies the value into a synthetic field such as val$percent:
The stack frame of schedule holds percent and orderId; the heap object Scheduler$1 holds copies in val$percent and val$orderId plus this$0; after the method returns the frame is gone but the object and its copies remain
If you could reassign the local later, the copy and the original would silently disagree. Java forbids it: since Java 8 the variable needn't be declared final, but it must be effectively final (never reassigned).
How I’d say it in the interview
"The value is copied into the object, because the stack frame dies with the method. Two copies that could drift apart would be a bug factory, so the local must never change after it's captured."
Signals depth
Mentioning the synthetic val$x fields, and that outer fields aren't copied (they're read through this$0), so they can change.
Red flags
"Because of thread safety." That's a side benefit, not the reason.
Follow-ups interviewers ask next
What if the captured variable is a
Listand the lambda callslist.add()?That compiles: the reference is effectively final, the object it points to isn't. It's legal, and it's also a data race if the lambda runs on several threads.
How do you create an instance of an inner class from main?
Tests outer.new syntax, static context
Model answer, how to say it: unlock with the Java Pass
What's the difference between an anonymous class and a lambda? What does this print?
Tests this in lambdas, class files, invokedynamic
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Why is the Builder usually a static nested class?
Tests static nested default, private constructors, nest access
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Walk me through how you choose between static nested, inner, anonymous classes and lambdas.
Tests nested type selection, Effective Java Item 24
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
A heap dump shows 3,000 ReportPage objects still alive, each about 50 MB. The path to GC roots goes through this$0. What happened?
Tests inner class leak, GC roots, listeners
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What does this print?
Tests shadowing, Outer.this
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
A colleague writes int total = 0; orders.forEach(o -> total += o.amount());. What's wrong and how do you fix it?
Tests effectively final, streams, side effects
Model answer, how to say it: unlock with the Java Pass
Can an inner class declare static members? Can a static nested class read the outer's instance fields?
Tests Java 16 change, static context
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Someone "simplified" config setup with new HashMap<>() {{ put("timeout", 30); }} in a few hundred places. What does that cost?
Tests double-brace initialization, anonymous subclasses
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What are nestmates, and why did old stack traces show methods like access$000?
Tests JEP 181, synthetic accessors, private access
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
After each config reload, heap usage grows by about 200 MB and never comes back. Each reload creates a new TenantService that schedules a refresh with an anonymous Runnable. Walk me through it.
Tests anonymous class leak, executors, lifecycle
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
How does a lambda capture variables and this at the bytecode level?
Tests invokedynamic, LambdaMetafactory, hidden classes
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Deserialization of cached session objects started failing with InvalidClassException after a release that only added a small anonymous class. Why?
Tests inner class serialization, synthetic names
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Premium question set
Keep reading with the Java Pass
The rest of this question set is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.
From ₹199 · one-time payment · No auto-renewal