Constructors & "this"
A Constructor's Job
A constructor's only job is to put a new object into a valid state — every instance variable set to something usable. Its signature looks like a method's, but with two differences: no return type (not even void), and its name is always exactly the class's name.
public class Climber
{
private int motorPort;
public Climber(int motorPort) // same name as the class, no return type
{
this.motorPort = motorPort;
}
}
The Default Constructor
If you don't write any constructor at all, Java gives your class a free no-argument one that sets every instance variable to its type's default (0 for int, 0.0 for double, false for boolean, null for any reference type). The moment you write even one constructor yourself, that free one disappears — you have to cover every way you want objects of your class to be built.
public class Climber
{
private int motorPort; // if no constructor is written, this defaults to 0
}
Overloaded Constructors
Just like methods (Ch.4.1), a class can have several constructors, distinguished by their parameter lists — this is the same overloading concept:
public class Climber
{
private int motorPort;
private double climbSpeed;
public Climber(int motorPort)
{
this.motorPort = motorPort;
this.climbSpeed = 0.5; // a sensible default
}
public Climber(int motorPort, double climbSpeed)
{
this.motorPort = motorPort;
this.climbSpeed = climbSpeed;
}
}
this: Referring to the Current Object
Inside an instance method or constructor, this refers to the specific object the method/constructor is currently running for. It's most useful when a parameter shares a name with an instance variable — this.motorPort unambiguously means "this object's motorPort," while bare motorPort means the parameter:
public Climber(int motorPort)
{
this.motorPort = motorPort; // left side: instance variable. right side: parameter.
}
Without this, motorPort = motorPort; would just assign the parameter to itself — the instance variable would never actually get set.
this only exists inside instance methods and constructors — a static method has no current object to refer to, so it has no this (Ch.7.4).
this as an Argument
this is a real reference, usable anywhere an object reference is expected — including passing the current object into another method or constructor:
public class Climber
{
private double currentHeight;
public void reportTo(ClimbLogger logger)
{
logger.record(this); // pass the current Climber object itself
}
}
Common Pitfalls
- Forgetting
thiswhen a parameter shadows an instance variable.motorPort = motorPort;(nothis) does nothing useful — it assigns the parameter to itself, leaving the actual instance variable untouched. - Assuming a class always gets a free default constructor. Only true if you write zero constructors yourself. Write even one, and you're responsible for every constructor your class needs.
Key Takeaways
- A constructor's signature has no return type and always matches the class's name; its job is to leave the object in a valid state.
- With no constructor written, Java provides a default one that zeroes/nulls every instance variable — writing any constructor yourself removes that default.
- Constructors can be overloaded, just like methods, distinguished by parameter lists.
thisrefers to the current object inside an instance method or constructor — essential for disambiguating a parameter from an instance variable of the same name.thiscan be passed as an argument, like any other object reference. Static methods have nothis.
Deck context: mechacoder-test/src/lessons/java-1.js, slide 8 ("CONSTRUCTORS")