Home / Programs / Solution of the previous problem,different name instance variable and local variable
🚀 Programming Example

Solution of the previous problem,different name instance variable and local variable

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

💻 Program Code

 //Solution of the previous problem by this keyword

class StudentClass{
int rollno;
String name;
float age;

StudentClass(int r,String n,float a){
rollno=r;
name=n;
age=a;
}

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


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

	StudentClass s1=new StudentClass(1,"Rahim",21.0f);
	StudentClass s2=new StudentClass(2,"Ram",21.5f);
	s1.display();
	s2.display();
 }
}

/*

If local variables(formal arguments) and instance variablesare different,
there is no need to use this keyword like in the following program:

If parameters (formal arguments) and instance variables are same,
we are using this keyword to distinguish local variable and instance variable.

*/

                        

🖥 Program Output

1 Rahim 21.0
2 Ram 21.5
Press any key to continue . . .
                            
📚 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.