Appearance
Lesson 01 · Classes & Objects
Objectives
After this lesson you will be able to:
- Declare fields, methods, and constructors, and use
this. - Tell static from instance members, and predict initialization order.
- Overload methods and resolve which overload is chosen.
Fields, methods, constructors
A class bundles state (fields) and behavior (methods). A constructor has the class's name and no return type; if you write none, the compiler supplies a no-arg default constructor — but only if you declare no constructor at all.
java
class Point {
int x, y; // instance fields (default to 0)
Point(int x, int y) { // constructor
this.x = x; // `this` disambiguates field from parameter
this.y = y;
}
Point() { this(0, 0); } // constructor chaining via this(...)
}Exam trap
Once you declare any constructor, the default no-arg constructor is gone. new Point() then fails to compile unless you also declare a no-arg constructor. A this(...) call must be the first statement in a constructor.
Static vs instance members
A static member belongs to the class, shared by all instances; an instance member belongs to each object. Static methods cannot use this or access instance members directly.
java
class Counter {
static int total; // one shared slot
int id; // one per object
Counter() { id = ++total; }
static int count() { return total; } // no access to id here
}Initialization order
When a class loads and an object is built, members initialize in a fixed order:
- Static fields and static initializer blocks, in source order — once, when the class first loads.
- Instance fields and instance initializer blocks, in source order — every time an object is created.
- The constructor body.
java
class Demo {
static int s = log("static field");
static { log("static block"); }
int i = log("instance field");
{ log("instance block"); }
Demo() { log("constructor"); }
}
// First new Demo(): static field, static block, instance field, instance block, constructor
// Second new Demo(): instance field, instance block, constructor (statics already done)Overloading
Overloading = same method name, different parameter lists. The compiler picks the most specific applicable overload at compile time, preferring an exact match, then widening, then boxing, then varargs.
java
void f(int x) { }
void f(long x) { }
void f(Integer x) { }
void f(Object x) { }
f(5); // calls f(int) — exact match wins over widening (long) and boxing (Integer)Gotcha
Overload resolution prefers widening over boxing, and boxing over varargs. So a short argument with overloads f(int) and f(Short) picks f(int) (widening) over f(Short) (boxing). Return type alone does not distinguish overloads.
SDET note
Constructors and overloads are where "looks right, compiles wrong" AI suggestions hide — a removed no-arg constructor or an ambiguous overload breaks call sites elsewhere. Let the compiler and a unit test, not a glance, confirm the right overload is chosen.
Key Takeaways
- A constructor names the class and has no return type; declaring any constructor removes the default no-arg one.
this(...)/super(...)must be the first statement. staticmembers belong to the class (shared, nothis); instance members belong to objects.- Init order: static fields/blocks once at class load, then per object the instance fields/blocks, then the constructor body.
- Overloading resolves at compile time to the most specific match: exact → widening → boxing → varargs. Return type never distinguishes overloads.
Lesson Quiz
Which call fails to compile?
class P { P(int x) { } } // ... new P(1); new P();What is printed on the SECOND new Demo() call?
static int s = log("S"); static { log("SB"); } int i = log("I"); { log("IB"); } Demo() { log("C"); }Which overload does f(5) call?
void f(long x) {} void f(Integer x) {} void f(Object x) {}Where must a this(...) constructor call appear?
Can a static method access an instance field directly?
Next: Inheritance & Polymorphism. Run the matching code in labs/src/main/java/com/jse21/m03_oop/.