Curriculum
Constructors and this Keyword in Java are important Object-Oriented Programming concepts used for object initialization and accessing current object properties. Constructors help initialize objects automatically, while the this keyword helps refer to the current object inside a class.
In this Core Java course in Jaipur, students will learn constructors in Java, types of constructors, constructor overloading, default constructor, parameterized constructor, and the use of this keyword in Java programming.
Constructors and this keyword are widely used in:
Understanding constructors and this keyword in Java helps students build professional and scalable object-oriented applications.
A constructor in Java is a special method used to initialize objects.
Constructors are automatically called:
Constructors:
class ClassName {
ClassName() {
// constructor code
}
}
class Student {
Student() {
System.out.println("Constructor Called");
}
public static void main(String[] args) {
Student s1 = new Student();
}
}
Constructor Called
Here:
Student()
is constructor.
It executes automatically when:
new Student()
creates object.
Java mainly provides:
A constructor without parameters is called default constructor.
class Employee {
Employee() {
System.out.println("Default Constructor");
}
public static void main(String[] args) {
Employee e1 = new Employee();
}
}
Default Constructor
A constructor with parameters is called parameterized constructor.
class Student {
int id;
String name;
Student(int i, String n) {
id = i;
name = n;
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String[] args) {
Student s1 = new Student(101, "Rahul");
s1.display();
}
}
101 Rahul
Constructors help developers:
Multiple constructors with different parameters are called constructor overloading.
class Product {
Product() {
System.out.println("Default Constructor");
}
Product(String name) {
System.out.println(name);
}
public static void main(String[] args) {
Product p1 = new Product();
Product p2 = new Product("Laptop");
}
}
Default Constructor
Laptop
The this keyword in Java refers to:
It helps distinguish:
class Student {
int id;
Student(int id) {
this.id = id;
}
void display() {
System.out.println(id);
}
public static void main(String[] args) {
Student s1 = new Student(101);
s1.display();
}
}
101
Here:
this.id
refers to instance variable.
And:
id
refers to constructor parameter.
The this keyword is used for:
class Demo {
void display() {
System.out.println("Display Method");
}
void show() {
this.display();
}
public static void main(String[] args) {
Demo d1 = new Demo();
d1.show();
}
}
Constructors can call another constructor using:
this()
class Example {
Example() {
this(10);
System.out.println("Default Constructor");
}
Example(int x) {
System.out.println(x);
}
public static void main(String[] args) {
Example e1 = new Example();
}
}
10
Default Constructor
Constructors initialize:
Constructors initialize:
Constructors initialize:
Constructors initialize:
Constructors provide:
| Constructor | Method |
|---|---|
| Initializes object | Performs operations |
| Same name as class | Any valid name |
| No return type | Has return type |
Incorrect:
void Student() {
}
Constructor must match class name exactly.
Variable shadowing may occur without this keyword.
Understanding constructors and this keyword helps students:
In this lesson, students learned:
These concepts are essential for Object-Oriented Programming and Java software development.
A constructor is a special method used to initialize objects.
Constructors automatically initialize object data during object creation.
A constructor that accepts parameters is called parameterized constructor.
this keyword refers to current object of class.
Yes, Java supports constructor overloading.
Constructors initialize objects, while methods perform operations.
WhatsApp us