Serialization & Reflection
Serialization and deserialization, the transient keyword, and the Reflection API.
4 questions
JuniorTheoryCommonWhat does the transient keyword do to a field during serialization?
What does the transient keyword do to a field during serialization?
transient marks an instance field to be skipped when an object is serialized — its value is not written to the byte stream. Use it for secrets like passwords, caches, or derived values you can recompute. On deserialization the field is not restored from the stream, so it gets the type's default: null, 0, or false.
Common mistakes
- ✗Assuming a
transientfield keeps its pre-serialization value after deserialization instead of getting the type default - ✗Marking a field
transientto hide it but forgetting it can still be read in memory before serialization - ✗Confusing
transient(skips serialization) withvolatile(controls visibility across threads)
Follow-up questions
- →How can you still restore a
transientfield after deserialization? - →Why are
staticfields also not part of an object's serialized state?
MiddleTheoryCommonWhat is the Reflection API in Java, and what are its trade-offs?
What is the Reflection API in Java, and what are its trade-offs?
Reflection lets code inspect and manipulate classes, methods, and fields at runtime rather than at compile time. From a Class object you list members with getDeclaredMethods, then invoke a method or read a field by name. It powers frameworks — DI, serializers, ORMs — that work with unknown types. The costs: it breaks encapsulation (reaching private members), is slower than direct calls, and defers errors to runtime.
Common mistakes
- ✗Using reflection where ordinary polymorphism or an interface would be simpler and type-safe
- ✗Forgetting reflective access of
privatemembers needssetAccessible(true)and can be blocked by a security manager or module rules - ✗Ignoring the runtime cost and using reflection inside tight, hot loops
Follow-up questions
- →How do Java annotations and reflection work together to drive a framework?
- →Why does the Java Platform Module System restrict reflective access by default?
MiddleTheoryCommonWhat is serialization in Java, and how does serialVersionUID matter?
What is serialization in Java, and how does serialVersionUID matter?
Serialization converts an object's state into a byte stream so it can be stored or sent over a network; the class must implements Serializable, and you write it via ObjectOutputStream. Deserialization rebuilds the object from those bytes. serialVersionUID is a version stamp checked on read — if it differs from the loaded class, it throws InvalidClassException. transient and static fields are excluded.
Common mistakes
- ✗Omitting an explicit
serialVersionUIDand gettingInvalidClassExceptionwhen the auto-generated one shifts - ✗Assuming all fields are serialized, forgetting
transientandstaticare excluded - ✗Thinking the no-arg constructor runs on deserialization — for a
Serializableclass it does not
Follow-up questions
- →How do
writeObject/readObjectlet a class customize its own serialization? - →Why is Java's built-in serialization often considered a security risk?
SeniorPerformanceOccasionalWhy is a reflective Method.invoke slower than a direct call, and how do you cut that cost?
Why is a reflective Method.invoke slower than a direct call, and how do you cut that cost?
Every invoke pays what a direct call does not: an access check, boxing of each primitive argument into an Object[] and of the return value, and a call site the JIT treats as opaque — so it is not inlined, and the optimizations that ride on inlining are lost. Member lookup also copies Method objects. Cut the cost by caching the Method, calling setAccessible(true), or moving to MethodHandle/LambdaMetafactory, which the JIT can inline.
Common mistakes
- ✗Blaming the whole cost on
setAccessible, ignoring boxing and lost inlining - ✗Calling
getDeclaredMethodinside the hot loop instead of caching theMethod - ✗Assuming the JIT eventually optimizes a reflective call into a direct one
Follow-up questions
- →Why can a
MethodHandlebound to astatic finalfield be inlined whileMethod.invokecannot? - →How does a serializer amortize reflection cost when it maps thousands of objects?