Home / Programs / Note:Call to this() must be the first statement in constructor.
🚀 Programming Example

Note:Call to this() must be the first statement in constructor.

👁 1,527 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

💻 Program Code

 class Studentclass{
int rollno;
String name,course;
float age;

Studentclass(int rollno,String name,String course){
this.rollno=rollno;
this.name=name;
this.course=course;
}

Studentclass(int rollno,String name,String course,float age){
this.age=age;
this(rollno,name,course);//Compile Time Error: Call to this must be first statement in constructor
}

void display(){
	System.out.println(rollno+" "+name+" "+course+" "+age);
	}
}

class MainClassOfStudent{
public static void main(String args[]){

	Studentclass obj1=new Studentclass(111,"RAHIM","C");
	Studentclass obj2=new Studentclass(112,"RAM","java",21.5f);
	obj1.display();
	obj2.display();
	}
}

/*
Note: Call to this() must be the first statement in constructor.
otherwise it will give Compile Time Error:
*/

                        

🖥 Program Output

C:\Users\Rumman Ansari\Documents\MainClassOfStudent.java:14: error: call to this must be first statement in constructor
this(rollno,name,course);//Compile Time Error: Call to this must be first statement in constructor
    ^
1 error
                            

📘 Explanation

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.