World 8 of 26 · Intermediate

🏗️ Classes & Objects

14 Java concepts, each explained step by step with real code, a common trap and practice questions.

  1. Class vs objectA class is a blueprint; objects are instances created with new.
  2. Fields & stateInstance fields hold each object's state.
  3. ConstructorsSame name as class, no return type, the implicit default constructor and when it disappears.
  4. The this keywordRefers to the current object; disambiguates fields from parameters.
  5. Constructor chaining with this()Calling another constructor; must be the first statement (before Java 25 flexible bodies).
  6. Access modifierspublic, protected, package-private (default), private.
  7. EncapsulationPrivate fields, controlled access through methods, validating invariants.
  8. Static fields & methodsOne copy per class shared by all instances.
  9. Initializer blocks & init orderStatic blocks once at class init; instance blocks before the constructor body.
  10. References, aliasing & nullTwo variables can point to one object; NullPointerException and helpful NPE messages.
  11. Object lifetime & garbage collectionUnreachable objects are collected automatically; no destructors; finalize is deprecated.
  12. toString()Default ClassName@hash and overriding it for readable output.
  13. Packages & importsPackage naming, import, static import, java.lang is automatic, name clashes.
  14. Immutable classesfinal class, private final fields, no setters, defensive copies.