0% found this document useful (0 votes)
3 views15 pages

Constructor in Java

A constructor in Java is a special method used to initialize objects, sharing the same name as the class and having no return type. It is automatically called when an object is created and can be either a default or parameterized constructor, with the ability to overload constructors. Key rules include that constructors cannot be abstract, final, or static, and they help in organizing code and avoiding uninitialized objects.

Uploaded by

khadizatulkhushi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

Constructor in Java

A constructor in Java is a special method used to initialize objects, sharing the same name as the class and having no return type. It is automatically called when an object is created and can be either a default or parameterized constructor, with the ability to overload constructors. Key rules include that constructors cannot be abstract, final, or static, and they help in organizing code and avoiding uninitialized objects.

Uploaded by

khadizatulkhushi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CONSTRUCTOR IN JAVA

Beginner Friendly Guide with Examples

Khadizatul Khushimoni | Introduction to Java


WHAT IS CONSTRUCTOR?
A constructor is a special method class
used to initialize objects.

It has the same name as class


(Name = class name)
Constructor
It does not have a return
type(Not even void)

It is automatically called when


Object
an object is created
Watch this video to understand clearly:
Example:Constructor
Explanation:
class Car {
Car( ) { Car( ) is the constructor of the Car class.
[Link]("Car created!") ; It has the same name as the class and no return
} type.
} The constructor runs automatically when c1 is
Car c1 = new Car( ) ; created.
The line [Link]("Car created!"); prints
the output to the screen.
Output: Car created!
Output: Car created!
RULES OF CONSTRUCTOR
A constructor must have the same
name as the class.
A constructor has no return type.

It is called automatically when an


object is created.

A constructor cannot be abstract,


final, or static.

Constructors can be overloaded.


WHY CONSTRUCTOR IS IMPORTANT?

Example:
To initialize object data class Car {
To avoid uninitialized objects String color;
To make object creation easier Car(String c) { color = c; }
}
To keep code organized
Car c1 = new Car("Red");
To control how objects are [Link]([Link]); // Output:
created Red
TYPES OF CONSTRUCTOR
Default Constructor:

No parameters
Java automatically creates one if you don’t write any constructor

Parameterized Constructor:

Constructor with parameters


Helps set custom values during object creation
Default Constructor Example
Explanation:
class Car {
Car( ) {
[Link]("Car created!") ;
Car() is the default constructor
}
} It prints a message when the object is created
Output: Car created!
public class Main {
public static void main(String[ ] args) {
Car c1 = new Car( ) ;
}
}
Parameterized Constructor Example

Explanation:
class Car {
String model;
This constructor receives a value
Car(String m) { It allows creating objects with different
model = m ;
}
values
} Output: Toyota

public class Main {


public static void main(String[ ] args) {
Car c1 = new Car("Toyota") ;
[Link]([Link]) ;
}
}
Constructor Overloading
Constructor Overloading means creating multiple constructors in the same class with the
same name but different parameters.
It allows us to create objects in different ways.

class Car {
// Constructor 1 Car(String Car(String
Car()
Car( ) { model) model,int year)
[Link]("Default
No parameters
Constructor") ; One parameter Two parameter
}
// Constructor 2
Car(String model) {
[Link]("Model: " + model); Same constructor name + different parameter list = Overloading
}
// Constructor 3
Car c1 = new Car ();
Car(String model, int year) {
[Link]("Model: " + model + Car c2 = new Car(“Toyota”);
", Year: " + year) ; Car c3 = new Car(“BMW”,2022);
}
} Java automatically chooses the correct constructor based on the
arguments.
How Constructors Work Internally?
How it works internally?
When you write Car c = new Car(); Java first uses new to
Object is created using new keyword request memory.
Memory for the object is created in the Heap Area.
Then Java calls the constructor of Car.
The constructor initializes variables (default values or
Memory is allocated in Heap
provided values).
After the constructor finishes, the object becomes ready
to use.
Constructor of the class is called
automatically class Car {
Car( ) {


[Link]("Constructor called");
Object gets initialized with default or }
given values }

Car c = new Car( );

Output: Constructor called

Java → Allocates Memory → Calls Constructor → Initializes Object


Real-Life Analogy
Think of a constructor like a phone setup screen.
When you turn on a brand new phone:

It sets language
It sets time
It sets user preferences

This automatic setup process is similar to how a constructor


initializes an object. Watch this:
This initial setup = the constructor of the phone.

Phone setup →
Constructor
Settings → Object values
Avoid these Mistakes
❌ Constructor has a return type
❌ Constructor name doesn’t match class name
❌ Forgetting to create object using new
❌ Trying to make constructor static
❌ Confusing method and constructor
Summary

Constructor initializes objects


Same name as class
No return type
Runs automatically
Can be default or parameterized
Can be overloaded
THANK YOU
Presented by Khadizatul Khushimoni

You might also like