constructor in java | constructor overloading in java |

Опубликовано: 27 Май 2026
на канале: LearningPointAabid
27
9

A constructor in Java is a special method that is called when an object of a class is created. It is used to initialize the state of the object. .
When an object of the class Car is created, the constructor will be called automatically.
Even if you haven’t specified any constructor in the code, the Java compiler calls a default constructor. The default constructor is used to assign default states and values, such as 0, null, etc., to the object.
A constructor is syntactically similar to a method, but there are several differences between the two. Firstly, although it returns the current class instance, a constructor does not have any explicit return type. Secondly, it is invoked implicitly

Rules for creating constructors :-
The constructor‘s and class’s name must be identical (same)
You cannot define an explicit value to a constructor (explicit refers to something is that stated directly)
A constructor cannot be any of these: static, synchronized, abstract, or final
One thing to note here is that you can have a public, private, or protected constructor in Java by using access modifiers that control object creation.
Types of Constructors :-
Java constructors can be of two types, which are:
default constructors
Parameterized constructors
Constructor Overloading :-
Constructor overloading allows you to create multiple constructors with different parameter lists.

How to Copy Values Without Constructor in Java?
There is no copy constructor in Java. However, you can still copy values from one object to another by using a constructor. Here’s an example to copy values of an object using a constructor
Copying values with the use of a constructor example
Person p1 = new Person(age:12,name:”Yamu”);
Person p2 = new Person(p1);
Copying values without constructor example
Person p1 = new Person(age:12,name:”Yamu”);
Person p2 = new Person();
p2.age = p1.age;
p2.name = p1.name;

previous video link :-   • getter and setter method in java|  java oo...  
source code link :-