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 found

Prefer 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 new creates 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);
  }
}

FAQ: Java Constructors

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java constructors examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java constructors in this Java Java lesson (Java Constructors).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.