Back to the 2026 paper

Module 2: Arrays and Strings

20267m

Write a short note on Constructors and Object Creation.

Worked SolutionAI Assisted

Constructors and object creation

A constructor initializes an object's state. It has the same name as its class and no return type. Constructors may be no-argument, parameterized, or overloaded; if no constructor is declared, Java supplies a default no-argument constructor.

class Book {
    String title;
    Book(String title) { this.title = title; }
}
Book b = new Book("Networks");

new allocates an object, initializes its fields to default values, and invokes the selected constructor. this(...) calls another constructor in the same class and must be first; super(...) calls a superclass constructor and must also be first.

Similar questions

Budget AppAndroid App

Pocketly on Android

Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.