In this lesson, you will learn
A constructor’s signature consists of the constructor’s name (which is the same as the class name) and its parameter list (the types and order of parameters).
For example:
public MyClass(int x, String s) { // Signature: MyClass(int, String)
// ... initialization code ...
}
public MyClass() { // Signature: MyClass() (no parameters)
// ... initialization code ...
}
Constructors in Java are of the following types.
null
for objects, 0
for integers, false
for booleans, etc.).
class ClassName {
// Default constructor (provided by the compiler if no constructor is defined)
ClassName() {
// No body or initialization (default values are assigned)
}
}
Automatically Provided: If you do not define any constructor in your class, the Java compiler automatically adds a default constructor.
No Parameters: It takes no arguments.
Default Initialization: It initializes instance variables with their default values:
null
for object references.
0
for numeric types (int
, double
, etc.).
false
for boolean
.
\u0000
for char
.
package ch7;
class RectangleClass {
// Instance variables
double length;
double breadth;
// Default constructor
RectangleClass() {
// Initialize instance variables with default values
length = 1.0; // Default length
breadth = 1.0; // Default breadth
}
// Method to calculate the area of the rectangle
double calculateArea() {
return length * breadth;
}
}
class RectangleDefault{
public static void main(String[] args) {
// Creating a rectangle object using the default constructor
RectangleClass myRect = new RectangleClass();
// Output the default dimensions and area of the rectangle
System.out.println("Area: " + myRect.calculateArea());
}
}
Area: 1.0
Note: If no constructor is explicitly defined in a class, the Java compiler automatically provides a default no-argument constructor that initializes the object with default values (e.g., null
for objects, 0
for numeric types, false
for boolean
).
package ch7.l5;
class Book {
String title;
String author;
int pages;
// No-argument constructor
public Book() {
title = "Untitled";
author = "Unknown";
pages = 0;
}
public void display() {
System.out.println("Book: " + title +","
+ "Author: " + author + ", Pages: " + pages);
}
}
public class BookDefault {
public static void main(String[] args) {
Book book = new Book();
// Output will be "Book: Untitled, Author: Unknown, Pages: 0"
book.display();
}
}
Book: Untitled,Author: Unknown, Pages: 0
package ch7.l5;
class Computer {
String processor;
int ram;
boolean isGamingPc;
// No-argument constructor
public Computer() {
processor = "Intel Core i5";
ram = 8; // 8 GB RAM
isGamingPc = false;
}
public void displaySpecs() {
System.out.println("Processor: " + processor + ""
+ ", RAM: " + ram + "GB, Gaming PC: " + isGamingPc);
}
}
public class ComputerMain {
public static void main(String[] args) {
Computer c1 = new Computer();
Computer c2 = new Computer();
// Output will be "Processor: Intel Core i5,
// RAM: 8GB, Gaming PC: false"
c1.displaySpecs();
//Assigning new values to object i.e. c2
c2.processor="Intel Core i7";
c2.ram=16;
c2.isGamingPc=true;
c2.displaySpecs();
}
}
Processor: Intel Core i5, RAM: 8GB, Gaming PC: false
Processor: Intel Core i7, RAM: 16GB, Gaming PC: true
Here’s a table summarizing the differences between a constructor and methods in Java, along with their signatures:
Aspect | Constructor | Method |
---|---|---|
Purpose | Used to initialize an object when it is created. | Used to perform operations or provide behavior for an object. |
Name | Must have the same name as the class. | Can have any valid identifier name (except the class name). |
Return Type | No return type (not even void ). |
Must have a return type (void if it does not return anything). |
Signature | ClassName(parameters) |
returnType methodName(parameters) |
Invocation | Automatically invoked when an object is created using the new keyword. |
Explicitly invoked using the object reference or class name (for static). |
Overloading | Can be overloaded (multiple constructors with different parameters). | Can be overloaded (multiple methods with the same name but different parameters). |
Inheritance | Not inherited by subclasses. | Inherited by subclasses (unless private or overridden). |
Default | If no constructor is defined, a default constructor is provided by the compiler. | No default method is provided. |
Example | public MyClass(int x) { ... } |
public void myMethod(int x) { ... } |
Constructor:
Used to initialize the state of an object.
Cannot be static
, final
, or abstract
.
Does not return anything, not even void
.
Method:
Used to define the behavior of an object.
Can be static
, final
, or abstract
.
Must have a return type (or void
if it does not return anything).
Easy to understand
You must be logged in to submit a review.