Interview Q&A / Java
Java interview questions
Collections, the JVM, strings, exceptions and threads, the backbone of most campus placement rounds.
Prefer rapid revision? Practise Java short interview answers →
- What is the difference between final, finally and finalize in Java?
final is a keyword that stops change (a variable cannot be reassigned, a method cannot be overridden, a class cannot be extended). finally is a block that runs after try/catch, usually for cleanup. finalize() was a method the garbage collector might call before removing an object; it is deprecated for removal and should not be used.
BeginnerIn-depth3 min read - What does the static keyword mean in Java?
static means a member belongs to the class itself, not to any one object. A static variable has one shared copy, a static method can be called without creating an object, a static block runs once when the class is loaded, and a static nested class does not need an outer object.
IntermediateIn-depth3 min read - What are the four access levels in Java and who can see each one?
private is visible only inside the class. Default (no keyword) adds the same package. protected adds subclasses in other packages. public is visible everywhere.
BeginnerQuick answer1 min read - What does the transient keyword do in Java?
transient marks a field to be skipped during serialization. When the object is deserialized, that field gets its default value (null, 0 or false), which is useful for passwords, caches and values that can be recomputed.
BeginnerQuick answer1 min read - What is the difference between this and super in Java?
this refers to the current object, and this(...) calls another constructor of the same class. super refers to the parent class part of the object, and super(...) calls a parent constructor.
BeginnerQuick answer1 min read - Why can a static method not use non-static variables or this in Java?
A static method belongs to the class and can run without any object, so there is no this and no instance to read fields from. To use an instance member it must be given an object explicitly.
BeginnerQuick answer1 min read - Why is the main method public static void in Java?
public so the JVM can call it from outside, static so it can run without creating an object, void because the JVM does not use a return value, and String[] args carries the command-line arguments.
BeginnerQuick answer1 min read - In what order do static blocks, instance blocks and constructors run in Java?
A static block runs once when the class is first loaded. For every new object, instance initialiser blocks and field initialisers run first, in source order, and then the constructor body.
IntermediateQuick answer1 min read - What is a blank final variable in Java and how do you initialise it?
A blank final is a final field declared without a value. It must be assigned exactly once, in every constructor for an instance field or in a static block for a static one, otherwise the code does not compile.
IntermediateQuick answer1 min read - What is the difference between a static nested class and an inner class in Java?
An inner (non-static) class holds a hidden reference to an instance of its outer class and can use its members. A static nested class has no such link, so it can be created without an outer object, like an ordinary top-level class placed inside another for organisation.
IntermediateQuick answer1 min read