-A constructor is a special block of code that is called when an object is created
-Its main job is to initialize the object, to set up its internal state, or to assign default values to its attributes.
-This process happens automatically when we use the "new" keyword to create an object
-Constructors must have the same name as the class name
-No Return Type, Constructors do not have a return type, not even void
Types of Constructors in Java
Now is the correct time to discuss the types of the constructor, so primarily there are three types of constructors in Java are mentioned below:
Default Constructor
Parameterized Constructor
Copy Constructor
class Student{
int rollno;
String department;
String name;
int tam;
int eng;
Student(int rollno,String department,String name,int tam,int eng){
this.rollno=rollno;
this.department=department;
this.name=name;
this.tam=tam;
this.eng=eng;
}
void GetStudent(){
System.out.println("Rollno:"+rollno);
System.out.println("Name:"+name);
}
void GetDep(){
System.out.println("Department:"+department);
}
void GetMark(){
System.out.println("Tamil mark:"+tam);
System.out.println("English:"+eng);
}
public static void main(String args[]){
Student obj=new Student(202,"BSC","Arun",67,99);
obj.GetStudent();
obj.GetDep();
obj.GetMark();
}
}
REFERANCE:
https://www.geeksforgeeks.org/java/constructors-in-java/
Top comments (0)