Q&A & Flashcards Available

Access questions, answers and flashcards for this chapter

View Q&A
Infographic
Quick Navigation:
| | | |

Constructors

4.1 Introduction

  • Constructors are a special and different type of method in Java.
  • Their most distinguishing feature is that they share the exact same name as the class they belong to.

4.2 Constructors

  • A constructor is a special member method used for initializing and constructing the data members of an object.
  • It is automatically called (invoked) by the system whenever an object is created using the new operator.
  • Unlike regular methods, constructors cannot be manually invoked by the user.

4.2.1 Characteristics of a Constructor

  • A constructor will always have the exact same name as its class.
  • It does not have a return type, not even void.
  • A constructor cannot be declared as static or final.
  • It is usually declared as public so that it can be accessed outside the class.
  • It runs automatically at the moment of object creation.

4.2.2 Need of a Constructor

  • They are necessary to provide valid (legal) initial values to the data members of an object as soon as it is created.
  • Since data members are mostly kept private to achieve data hiding and abstraction, constructors are the best way to initialize these hidden values implicitly.

4.3 Declaring and Defining a Constructor

  • The syntax involves an access specifier (like public), followed by the class name, and a parameter list.
  • Programmers have the liberty to define constructors either with parameters or without parameters.

4.3.1 Constructor Vs Member Methods of a Class

Similarities:

  • Both can act as member methods to perform functions.
  • Both can take parameters.
  • Both require an access specifier.
  • Both support overloading (having multiple versions with different signatures).

Differences:

  • Name: Constructors must share the class name; methods can have any valid name chosen by the user.
  • Return Type: Constructors have no return type; methods must have a return type (or void).
  • Purpose: Constructors initialize variables; methods prevent repetition and increase code reusability.
  • Execution: Constructors run only once when the object is created; methods can be called repeatedly whenever required.

4.4 Types of Constructors

Constructors are broadly categorized into two types based on the parameters they take:

4.4.1 Default Constructor / Non-Parameterised Constructor

  • A constructor that takes zero parameters.
  • If you do not create any constructor in your program, the Java compiler automatically supplies a default constructor.
  • It initializes instance variables to their default primitive values (e.g., integers to 0, floating points to 0.0, and Strings to null).

4.4.2 Parameterised Constructor

  • A constructor that takes one or more parameters or arguments.
  • It accepts specific values provided by the user and initializes the data members with these corresponding values.
  • There is no limit to the number of parameters a parameterized constructor can have.

4.5 Constructor Overloading

  • When more than one constructor exists within a single class, it is called constructor overloading.
  • This allows a class to have one default constructor and multiple parameterized constructors simultaneously.
  • Each overloaded constructor must have a different signature (a different number, type, or order of parameters).

4.6 Using 'this' Keyword

  • The this keyword is used inside a constructor to refer to the current calling object.
  • It acts as a reference (stores the memory address) of the object that invoked the constructor.
  • The Java compiler often adds this implicitly, but programmers use it explicitly to resolve naming confusions when instance variables and local method variables share the exact same name.

4.6.1 Assigning An Object As A Reference Variable To Another Object

  • The new operator dynamically allocates memory for an object and returns a reference.
  • You can assign one object to another (e.g., Book b2 = b1;).
  • When this happens, both objects share the same memory location. Changing a value through one object will directly affect the other object.

4.6.2 Creating A Temporary Instance

  • A temporary instance is an object created without assigning it a variable name (e.g., new Room1(10,10,10).display();).
  • Values are held in memory only for as long as the statement is executing, and the memory is removed afterward.

4.6.3 Constructors within Constructors using 'this'

  • Constructors can be chained together. This means one constructor can call another constructor within the same class.
  • This is done by using the this() keyword as a method call (e.g., this(10, 20);).
  • During execution, control moves from one constructor to the next until all chained constructors have finished initializing the object.
Quick Navigation:
| | | |
1 / 1
Quick Navigation:
| | | |
Quick Navigation:
| | | |
Quick Navigation:
| | | |