Java Tutorial
Java Constructors
A constructor runs when an object is created with new. Use it to set fields so the object starts in a valid state instead of half-empty defaults.
Initialize on creation
A constructor has the same name as the class and no return type — not even void. When you writenew Player("Rin", 0), that constructor runs and can assign the fields before anyone else uses the object.
Example
class Player {
String name;
int score;
Player(String name, int score) {
this.name = name;
this.score = score;
}
}
public class Main {
public static void main(String[] args) {
Player p = new Player("Rin", 10);
System.out.println(p.name + " " + p.score);
}
}Overloaded constructors
You can provide several constructors with different parameter lists. A short form can call a fuller one withthis(...) so you do not duplicate assignment code.
Example
class Player {
String name;
int score;
Player(String name) {
this(name, 0);
}
Player(String name, int score) {
this.name = name;
this.score = score;
}
}
public class Main {
public static void main(String[] args) {
Player a = new Player("Rin");
Player b = new Player("Kai", 14);
System.out.println(a.name + " " + a.score);
System.out.println(b.name + " " + b.score);
}
}The default constructor
If you write no constructor, Java supplies a public no-argument one that leaves fields at their defaults. The moment you add any constructor of your own, that automatic no-arg version disappears — callers must match a constructor you declared.
Example — no matching constructor
class Player {
String name;
Player(String name) { this.name = name; }
}
// Player p = new Player(); // error: no suitable constructor foundPrefer constructors over a long stretch of field assignments in main. The class then guarantees every object is born with the values it needs.
Try It Yourself
Exercise: Write a Book class with title and pages, a constructor that sets both, and print one book created with new Book("Dune", 412).
Show solution
class Book {
String title;
int pages;
Book(String title, int pages) {
this.title = title;
this.pages = pages;
}
}
public class Main {
public static void main(String[] args) {
Book b = new Book("Dune", 412);
System.out.println(b.title + " (" + b.pages + ")");
}
}The constructor runs at new, so the object is ready before the next line.
Key Takeaways
- A constructor shares the class name and has no return type.
- It runs when
newcreates the object — ideal for required fields. - You can overload constructors;
this(...)chains to another constructor. - Declaring any constructor removes the automatic no-arg default.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Physics
Build a charged particle
A constructor forces mass and charge to be set when the object is born. No half-initialised Particle.
Example
class Particle {
double mass, charge;
Particle(double m, double q) {
mass = m;
charge = q;
}
}
public class Main {
public static void main(String[] args) {
Particle e = new Particle(9.11e-31, -1.6e-19);
System.out.println(e.charge);
}
}