Static vs instance member interview questions, with answers
The static keyword is small and shows up everywhere, so interviewers use it to find out whether a candidate understands the difference between a class and its objects. The questions are mostly compile-error questions — what happens when a static method reads an instance field, why main has to be static — plus the ordering question of when a static block runs.
Here they are, with the rule behind each. When you have read them, take the free OOP diagnostic — ten questions across all fifteen OOP topics, scored with the arithmetic shown.
The questions, with answers
1.What does static mean, and what is the difference between a static and an instance member?
A static member belongs to the class itself: there is one copy, shared by every object and existing even when no object does. An instance member belongs to each object: every new object gets its own copy. So a static counter incremented in the constructor counts how many objects have been created — two constructions leave it at 2 — while an instance counter would be 1 in each object. Static members are accessed through the class name, Ticket.issued; accessing them through an object reference compiles in Java but is misleading, because it still touches the single shared copy. Constants, caches, registries and utility methods are static; anything that describes one object's state is not.
class Ticket { static int issued = 0; // one copy for the class int number; // one per object Ticket() { issued++; number = issued; } } // new Ticket(); new Ticket(); → Ticket.issued is 2; the tickets are numbered 1 and 22.Why can't a static method access instance variables directly?
Because a static method runs without an object. An instance variable lives inside some particular object, and a static method has no this reference to say which one — so reading x from a static method is a compile error, "non-static variable x cannot be referenced from a static context". The fix is to give the method an object to work with: pass one in, or create one, and read obj.x. Likewise a static method cannot call an instance method or use this or super. The reverse is fine: instance methods can read static members freely, because the class's single copy is always there. Every "why doesn't main see my fields" question is this rule.
3.In what order do static blocks, instance blocks and constructors run?
A static block runs once, when the JVM initialises the class — the first time it is actively used: an instance is created, a static method is called or a static field is accessed — and never again, no matter how many objects follow. Instance initialiser blocks and field initialisers run every time an object is constructed, after the superclass constructor and before the rest of the constructor body. The full order for new Child(): Parent's static initialisers (once), Child's static initialisers (once), Parent's instance initialisers, Parent's constructor body, Child's instance initialisers, Child's constructor body. Interviewers give a class with prints in each spot and ask for the output; write that sequence down before you answer.
class Demo { static { System.out.println("static block"); } // once, at class init { System.out.println("instance block"); } // every construction, before the constructor body Demo() { System.out.println("constructor"); } } // new Demo(); new Demo(); // static block / instance block / constructor / instance block / constructor4.Why is main declared static in Java?
Because the JVM has to call it before any object of the class exists, and only a static method can be called without an object. If main were an instance method the JVM would first need to construct an instance — with which constructor, with what arguments? — so the language sidesteps the question by requiring public static void main(String[] args): public so the JVM can reach it from outside the package, static so no object is needed, void because the exit code is set separately, and the String array for command-line arguments. Declare it non-static and, on the classic launcher, the JVM reports that no main method was found. Java 25 relaxed this for small programs by allowing an instance main() in compact source files, but the static signature is what every interview expects and what frameworks look for. C# has the same rule for Main; Python has no such requirement because module-level code just runs.
5.What is a static nested class, and how is it different from an inner class?
In Java a nested class declared static is just a class that lives inside another's namespace: it has no reference to an instance of the outer class, so it can be created with new Outer.Nested() and can only touch the outer class's static members. An inner (non-static) class carries a hidden reference to the enclosing instance, is created through outer.new Inner(), and can read the outer object's fields. That hidden reference is why inner classes are a memory-leak hazard — an inner class instance keeps its outer object alive — and why Map.Entry, Node in a linked list and builders are usually static nested classes. The rule of thumb: nest statically unless the class genuinely needs the enclosing object.
6.Where do static and instance members live in memory, and when are they created?
Static fields are allocated once per class when the class is loaded and initialised — conceptually in the JVM's method area; in HotSpot they sit alongside the class's java.lang.Class object on the heap — and they live until the class is unloaded, which for most applications means until the program ends. Instance fields are allocated inside each object on the heap when the object is constructed and are reclaimed with it by the garbage collector. Methods, static or not, are code and are stored once regardless. Two consequences interviewers ask about: a static field that references a large object keeps it alive for the whole run — the usual cause of a "leak" in a Java program — and static state is shared across every thread, so it needs synchronisation that instance state confined to one thread does not.
7.Can static methods be overridden or made abstract, and what is method hiding?
Neither. A static method cannot be abstract, because abstract means "to be implemented by a subclass and dispatched at runtime" and static dispatch never involves an object. And a static method is not overridden by a same-signature static method in a subclass — it is hidden: both exist, and which one runs is decided by the type of the reference at compile time, not the object's class. Parent p = new Child(); p.info() calls Parent.info() if info is static, and Child.info() if it is an instance method. Writing @Override above a static method is a compile error, which is the one-line proof. The same hiding rule applies to fields, static or not: fields are never dispatched dynamically.
8.When should a method be static, and what does overusing static cost?
Make a method static when it depends only on its parameters and on other static state — a pure function such as Math.max, a factory that creates instances, a utility that converts between types. Keep it an instance method when it reads or changes an object's fields, and when you might want to override it or swap the implementation. The costs of overusing static are the ones tests expose first: static methods cannot be overridden, so they cannot be mocked or replaced through an interface, and static mutable state is a hidden global that every test and every thread shares. A class that is nothing but static methods is fine as a utility; a domain class full of static methods is usually procedural code wearing a class declaration.
How the diagnostic asks it
One question from the OOP bank, exactly as a sitting would show it. The bank has 4 on static vs instance members and 60 across OOP.
What best describes a 'static' member in a class?
- 1It can only be accessed after at least one object of the class has been created.
- 2It is created fresh for every new object and stored separately in each object's memory.
- 3It automatically becomes private and cannot be accessed outside the class.
- 4It belongs to the class itself and is shared by all objects, rather than belonging to any single object instance.correct
Static members belong to the class rather than any instance, so a single copy is shared across all objects and can typically be accessed via the class name without creating an object. A member created fresh for every object and stored in each object's memory is an instance member; static members can be used before any object exists, so requiring an object first is wrong; and 'static' says nothing about visibility -- static and access modifiers (private/public) are independent concepts.
Measure it
Reading answers tells you what’s true. A diagnostic tells you what you get wrong.
10 OOP questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.