GamifiedGamified challengesPracticeRaceLeaderboardPricing

Learn data structures by watching them move, then doing it yourself. Press M anytime to toggle sound.

A product of Amypo Technologies, Coimbatore.

Learn

  • Fundamentals & OOPs
  • Data structures and algorithms
  • Aptitude
  • Learning path
  • How it works
  • About dotprep

Practice

  • Practice arena
  • Daily challenge
  • Gamified challenges
  • Race mode
  • Leaderboard

Interview prep

  • Java interview
  • Coding questions
  • Company questions
  • System design
  • Jobs & careers

You

  • Dashboard
  • Settings
  • Pricing
  • Create account

© 2026 dotprep

Content last updated 27 November 2026

GamifiedGamified challengesPracticeRaceLeaderboardPricing
Log inStart free

Free preview: 1 of 3 free topics used. You can open 2 more without an account.

Sign up free to unlock all 113
Fundamentals & OOPsObject-oriented programming

Constructors and this

beginnera special method that fills in a new object at the moment it is built
Before this: Classes and objects: you built a Student and filled in its fields by hand, one line at a time.

1. The problem

In the last lesson a student took three lines to build: one to make the object, one for the name and one for the marks. That is easy to get wrong. Forget the marks line and you have a student with no marks, and the program carries on as if nothing is missing. A college office avoids this with a simple rule: an admission form is accepted only when every line is filled. We want the same rule for objects.

How can a program make sure that every object is completely filled in at the moment it is built?

2. Picture it first

A constructor is the rule at the admission counter: a form is accepted only when every line is filled. You hand over your details, the clerk writes them on a fresh form on the spot, and only then are you a student. Nobody leaves the counter with a half-filled form.

Every part of the picture has a name in code:

In the pictureIn code
A blank admission formclass Student
The rule that no form is accepted with empty linesthe constructor
The details you give at the counterthe parameters: name, marks
The form in the clerk's hand right nowthis (Python: self)
The name line on that formthis.name
Giving your details and getting admittednew Student("Asha", 70)
An admitted studenta finished object: asha

3. Watch the story, built from zero

Story mode

Story mode

21 scenes · about 4 min · narrated
  • just created or just finished
  • being filled or read

In real life

📋blank formStudent

The blank admission form from the last lesson. It is not a student yet.

Classes and objects

📋Studentclass
  • name
  • marks

On the screen

Nothing printed yet

1class Student { // the blueprint
2 String name; int marks; // fields
3 Student(String name, int marks) { // the constructor
4 this.name = name; // this = the new object
5 this.marks = marks;
6 }
7}
8public class Main {
9 public static void main(String[] args) {
10 Student asha = new Student("Asha", 70);
11 Student ravi = new Student("Ravi", 55);
12 System.out.println(asha.name + " " + asha.marks);
13 System.out.println(ravi.name + " " + ravi.marks);
14 }
15}

1 / 21
EnglishThis lesson is in English for now

4. Filling fields by hand vs Constructors and this

Filling fields by handConstructors and this
Lines to build one objectOne to make it, then one for each fieldOne
Forgetting a fieldEasy, and nothing warns youThe constructor asks for every value
A half-filled objectExists between those linesIs never seen by the rest of the program
Where the filling code livesRepeated wherever an object is builtWritten once, in the class
Who runs itYou, line by lineIt runs by itself when the object is built

5. Common mistakes

  • Writing name = name inside the constructor.

    That copies the parameter onto itself, and the object's field stays empty. Write this.name = name, so the left side is the field.

  • Trying to call the constructor by name on an object, as if it were an ordinary method.

    You never call it yourself. It runs by itself, once, when the object is built.

  • Building an object without the values the constructor asks for: new Student().

    Hand over every value: new Student("Asha", 70). Most languages stop with an error if one is missing. JavaScript quietly leaves that field empty.

  • Giving the values in the wrong order: new Student(70, "Asha").

    The first value goes to the first parameter and the second to the second. Follow the order written in the constructor.

6. Where you'll meet it

Opening a bank account

A bank does not create an account without a name and an opening amount. An Account constructor asks for both.

Booking a train ticket

A ticket is built with the passenger, the train and the date together. A ticket that lacks one of them is never created.

A new contact on your phone

The app builds a Contact object from the name and number you typed, in one step, when you press Save.

Keeping the values correct afterwards

A constructor gives good starting values. Private fields and methods keep them good.

See that lesson

7. Check yourself

Answer these before you watch it move. Each answer explains itself.

Question 1

1.When does a constructor run?

Question 2

2.Inside the constructor, what does this.marks = marks do?

Question 3

3.A program builds 5 Student objects. How many times does the constructor run?

Question 4

4.The line ravi = new Student("Ravi", 55) is running. Inside the constructor, what does this mean?

In short

  1. 1A constructor is a special method in a class. It runs by itself each time an object is built. You never call it by name.
  2. 2The values you hand over when you build the object arrive in the constructor's parameters.
  3. 3Inside the constructor, the word this means the object being built. Python uses the word self instead.
  4. 4this.name = name copies the parameter called name into the object's own field called name.
  5. 5When the constructor finishes, the object is complete, and your variable is the name you reach it by.
  6. 6Every new object runs the same constructor again, with its own values.

On this page

  • The problem
  • Picture it first
  • Story mode
  • Compare
  • Common mistakes
  • Where you'll meet it
  • Check yourself
  • In short

Key facts

Runs
by itself, when an object is built
Job
give every field its first value
this
the object being built (Python: self)
How often
once for each object

When to use it

Almost every class has one. Write a constructor whenever an object needs starting values to make sense: a student needs a name, a bank account needs an opening amount, a train ticket needs a passenger and a date.