DEV Community

Hayes vincent
Hayes vincent

Posted on

Constructor(Java)

Types of Constructors:

** Default Constructor:**
If no constructor is explicitly defined in a class, the Java compiler automatically provides a public, no-argument default constructor. This constructor initializes instance variables to their default values (e.g., 0 for numeric types, null for objects, false for booleans)

** No-Argument Constructor:**
This is a constructor explicitly defined by the programmer that takes no arguments. It can be used to perform custom initialization logic when an object is created without specific initial values.

Parameterized Constructor:

This type of constructor accepts one or more arguments. It allows the programmer to initialize the object's attributes with specific values provided during object creation.

class Constactors{
    int phone;
    int age;
    String name;
Constactors(int phone,int age,String name){
this.phone=phone;
this.age=age;
this.name=name;
}
Constactors(int phone,int age){
this.phone=phone;
this.age=age;
}
Constactors(){
}
void GetData(){
System.out.println("phone:"+phone +"Age:"+age+"Name:"+name);
}
public static void main(String args[]){
Constactors obj=new Constactors();
obj.GetData();

}
} 
Enter fullscreen mode Exit fullscreen mode

Referance:

https://www.google.com/search?client=firefox-b-lm&q=what+is+constator+in+java

Top comments (0)