Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Java
  • Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
  • Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
  • Java Hello World with Maven: What the JDK Does and What Maven Adds
  • Java Variables, Keywords and Naming Rules
  • Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
  • Java Primitive Types and Memory: What int x = 5 Really Does
  • Java Strings and Text Blocks: Quotes, Concatenation and parseInt
  • Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary
  • Java Type Casting: Widening, Narrowing and Converting Text to Numbers
  • Java Practice Lab: A Cafe Bill with Variables, Operators and Casting
  • Java if, else and switch: Braces, Else-If Ladders and the Modern Switch
  • Java Loops: for, while, do-while and for-each
  • Java break, continue and Nested Loops
  • What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
  • Java Arrays: Indexes, Default Values, Loops and 2D Grids
  • Java Methods and Overloading: Parameters, return and Pass-by-Value
  • Java Scope, Code Blocks and the Call Stack
  • Read User Input in Java with Scanner (and the Leftover Enter Trap)
  • Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
  • Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
  • Java var: Let Java Infer the Type
  • Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
  • Java Classes and Objects: One Blueprint, Many Cars
  • Java Constructors and the this Keyword
  • Java Instance Methods, Getters, Setters and toString
  • Java static: Fields, Methods and Blocks Shared by the Whole Class
  • Java Packages and Imports: Every Class Has an Address
  • Java Access Modifiers and Encapsulation: Private Data, Public Methods
  • Java final and Immutable Objects: Assign Once, Share Safely
  • Java Strings as Objects: The String Pool, == vs equals, StringBuilder and printf
  • Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
  • Java Inheritance: extends, super and the Order Objects Are Built
  • Java Method Overriding, @Override and the Object Class
  • Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
  • Java Composition: Has-A vs Is-A
  • Java Abstract Classes: A Parent That Leaves One Blank
  • Java Interfaces: Contracts and Multiple Inheritance
  • Java Nested, Inner and Anonymous Classes
  • Java Enums with Fields, Methods and switch
  • Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
  • Java Records: A Whole Data Class in One Line
  • Java Sealed Classes and Interfaces
  • Java Pattern Matching with instanceof and switch
  • Java Record Patterns: Unpacking a Record
  • Java equals and hashCode: Why a HashSet Needs Both
  • From Arrays to Collections: The Java Collections Framework Map
  • Java Wrapper Classes and Autoboxing
  • Java ArrayList: The Stretchy Array
  • Java LinkedList vs ArrayList: Nodes or an Array
  • Java Sets: HashSet, LinkedHashSet and TreeSet
  • Java Sorting with Comparable and Comparator
  • Java Maps: HashMap, LinkedHashMap and TreeMap
  • Java Stacks, Queues and Deques: ArrayDeque and PriorityQueue
  • Choosing a Java Collection: Immutable and Sequenced Collections
  • Java Exception Handling: try, catch and finally
  • Java throw, throws, Checked vs Unchecked and Custom Exceptions
  • Java try-with-resources and Clean Error Handling
  • Java Generics: Type-Safe Containers
  • Java Generic Methods, Bounded Types and Wildcards
  • Java Lambdas and Functional Interfaces
  • Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
  • Java Optional: Saying Maybe Without null
  • Java Streams I: filter, map and reduce
  • Java Streams II: Collectors, groupingBy and flatMap
  • Why Concurrency: Processes, Threads and the Main Thread in Java
  • Creating Threads in Java: Thread, Runnable and Lambdas
  • Java Thread Lifecycle: sleep, join and interrupt
  • Java Race Conditions and synchronized
  • Java volatile and Atomic Variables
  • Java ExecutorService and Thread Pools
  • Java Callable, Future and CompletableFuture
  • Java Locks, Deadlock and tryLock
  • Java Concurrent Collections and Virtual Threads
  • Why Slow Work Belongs off the Request Thread: 202 Accepted and a Job Id in Spring Boot
  • Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
  • @Async, Named TaskExecutors and Structured Concurrency in Spring Boot
  • Java Files and Directories with NIO.2: Path and Files
  • Reading and Writing Text Files in Java
  • Java Structured Data: Reading and Writing JSON and CSV
  1. Blog
  2. /
  3. Java
  4. /
  5. Object-Oriented Programming

Java Constructors and the this Keyword

A constructor decides how an object is born: new Car("Toyota", 42.5) arrives filled in. this.brand vs brand, a second constructor that calls this(brand, 50.0), and why new Car() stops compiling once you write your own.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Object-Oriented Programming

What you will learn

  • Write a constructor: same name as the class, no return type, runs on new
  • Use this.brand to tell a field from a parameter with the same name
  • Overload constructors and chain them with this(...)
  • Explain why writing any constructor removes the free default one

Before you start

  • Java classes and objects
  • Java methods and overloading

Suppose you want a rule: nobody can create a car without giving it a brand and some fuel, because a car with no information is useless. That rule is what a constructor gives you. It decides how an object is created.

If you write no constructor, Java adds an empty one for you, the default constructor. It does nothing, and every field keeps its default value. Write your own and Java adds nothing.

Write a constructor

package org.studyeasy.s03l02;

// A constructor hands you a car that is ready to drive
public class Car {
    String brand;
    int speed;
    double fuel;

    // this.brand is the field; brand alone is the parameter
    Car(String brand, double fuel) {
        this.brand = brand;
        this.fuel = fuel;
    }

    // Brand only: pass the work on, with a full tank
    Car(String brand) {
        this(brand, 50.0);
    }
}

A constructor looks like a method with two differences: it has exactly the same name as the class (capital C included) and no return type. It runs once, when new builds the object.

The parameter is called brand and so is the field. Inside the constructor, plain brand means the parameter, the value coming in from outside. this.brand means this object's field. So this.brand = brand; copies the incoming value into the field. Write fuel = fuel; by mistake and you just assign the parameter to itself: the field stays 0.0.

new Car("Toyota", 42.5): first a blank car appears with brand null, speed 0, fuel 0.0. The constructor receives brand Toyota and fuel 42.5 and copies them into the fields. Speed was not passed in, so it stays 0. new Car("Toyota", 42.5) 1. blank car brand null, speed 0 fuel 0.0 2. constructor this.brand = "Toyota" this.fuel = 42.5 3. ready car Toyota, 42.5 L speed stays 0 The values you pass in are copied into the new car before the next line runs.
What new does: build a blank object, then run the constructor on it.

One line, a ready car

package org.studyeasy.s03l02;

public class Ex01_ReadyToDrive {
    public static void main(String[] args) {
        // One line builds the car AND fills it in
        Car taxi = new Car("Toyota", 42.5);
        System.out.println("Brand: " + taxi.brand);
        System.out.println("Fuel:  " + taxi.fuel + " L");
        // speed was not passed in, so it keeps its default
        System.out.println("Speed: " + taxi.speed + " km/h");
    }
}
Brand: Toyota
Fuel:  42.5 L
Speed: 0 km/h

Arguments go in by position: Toyota is the brand, 42.5 the fuel. Compare that with setting each field by hand. Swap the order, new Car(42.5, "Toyota"), and it doesn't compile.

A second constructor: overloading and this(...)

You can write several constructors with different parameters. That's constructor overloading, and Java picks one by the arguments. The second constructor above takes only a brand and calls this(brand, 50.0). With brackets, this(...) calls another constructor of the same class. So the short constructor does no work of its own: it hands the brand and a full 50-litre tank to the full constructor, and no code is repeated.

package org.studyeasy.s03l02;

public class Ex02_OverloadedConstructors {
    public static void main(String[] args) {
        // Same class, two constructors: Java picks by the arguments
        Car rental = new Car("Honda");        // one argument
        Car racer = new Car("Kia", 30.0);     // two arguments

        System.out.println(rental.brand + ": " + rental.fuel + " L");
        System.out.println(racer.brand + ": " + racer.fuel + " L");
    }
}
Honda: 50.0 L
Kia: 30.0 L

One argument picks the short constructor, so the Honda gets the default 50.0. Two arguments pick the full one, so the Kia gets 30.0. new Car("Kia", 30.0, 4) fails: no constructor takes three arguments.

Write one, lose the free one

package org.studyeasy.s03l02;

public class Ex03_LostDefault {
    public static void main(String[] args) {
        // Car wrote its own constructors, so Java adds no free Car()
        Car spare = new Car("Spare");
        System.out.println(spare.brand + ": " + spare.fuel + " L");
    }
}
Spare: 50.0 L

Because Car has its own constructors, Java no longer adds the empty Car(). Write new Car() and the compiler says "no suitable constructor found for Car". The fix is to pass what it asks for, like new Car("Spare"), or write a no-argument constructor yourself if you really need one.

Recap

IdeaRemember
ConstructorSame name as the class, no return type, runs on new
Default constructorThe empty one Java adds, only if you write none
OverloadingSeveral constructors, different parameters
this.brandThe object's own field, not the parameter
this(...)Calls another constructor of the same class

Next: instance methods, getters, setters and toString.

Keep going in Java · 24 of 79

Previous
Java Classes and Objects: One Blueprint, Many Cars
Next post
Java Instance Methods, Getters, Setters and toString
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Related posts

Java Classes and Objects: One Blueprint, Many Cars

Six loose variables for two cars doesn't scale. A Car class is the blueprint; new builds each car with its own brand, speed and fuel. Plus field defaults, and why two variables can point at one car.

Java Instance Methods, Getters, Setters and toString

Give a Car class behaviour: accelerate() changes only this car's speed, private fields are reached through getSpeed and setSpeed, and toString turns Car@1b6d3586 into 'Toyota doing 60 km/h'.

Java static: Fields, Methods and Blocks Shared by the Whole Class

A static field lives once on the class: every Car ticks the same carsBuilt counter while keeping its own brand. Static methods need no object, a static block runs before main, and main is static so the JVM can start with no object.

Java Packages and Imports: Every Class Has an Address

Two classes called Order, one in shop and one in shipping: the package keeps them apart. How the package line maps to folders, plain and static imports, using both Orders in one file, and the clash when you import both.

On this page

  • Write a constructor
  • One line, a ready car
  • A second constructor: overloading and this(...)
  • Write one, lose the free one
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.