Java Constructor.
Date : 10-April-2025 Constructor In java methods are typical one and we wrote every code in a method. That method are varies in nature. Some methods works same in all time, not the inner content of the method, before executing the methods content, the methods all are working under a same priciple except some methods. one of those except methods is Constructor. Constructor is a special type of method which is only invoked by an instance at that time of its creation. Constructor is only invoked at the time of object creation. Instance of class is the only invoker of a Constructor. Without object we cannot able to call any constructor. Rules to define a constructor. A constructor name must be same as its classname. A constructor must not have any method return type. A constructor can be overloaded. If no constructor is defined in the class, a default constructor will be assigned to it by JVM. Note: The default constructor is a no argument constructor. e.g, class ConstructorSample{ ConstructorSample(){ System.out.println("This is a constructor"); } public static void main(String[] args){ ConstructorSample cs = new ConstructorSample(); } } output: This is a contructor.

Date : 10-April-2025
Constructor
In java methods are typical one and we wrote every code in a method. That method are varies in nature.
Some methods works same in all time, not the inner content of the method, before executing the methods content, the methods all are working under a same priciple except some methods.
one of those except methods is Constructor. Constructor is a special type of method which is only invoked by an instance at that time of its creation. Constructor is only invoked at the time of object creation. Instance of class is the only invoker of a Constructor.
Without object we cannot able to call any constructor.
Rules to define a constructor.
- A constructor name must be same as its classname.
- A constructor must not have any method return type.
- A constructor can be overloaded.
- If no constructor is defined in the class, a default constructor will be assigned to it by JVM. Note: The default constructor is a no argument constructor.
e.g,
class ConstructorSample{
ConstructorSample(){
System.out.println("This is a constructor");
}
public static void main(String[] args){
ConstructorSample cs = new ConstructorSample();
}
}
output:
This is a contructor.