Skip to main content

Java this keyword


 

this keyword in Java programming language is a reference variable that refers to the current object. this in Java is  a reference to the current object, whose method is being called upon. You can use this keyword to avoid naming conflicts in the method/constructor of your object.


What are the usages of this keyword in Java Programming Language.


1) this can be used to refer the current class instance variable.


2) this can be used to invoke current class method.


3) this() method ca be used to invoke current class constructor.


4) this can be passed as an arguments in the method call.


5) this can be passed as an arguments in the constructor call.


6) this can be used to return the current instance from the method.


Lets see an example to understand how actually this keyword works..


class student

{

int roll_no;  //  variable declaration 

String Student_name; // variable declaration

float fees; //variable declaration


student(int roll_no, String Student_name, float fees) // parameterized constructor

{

roll_no = roll_no;

Student_name = Student_name;

fees = fees;

}

void display()  //java method

{

System.out.println(roll_no+" "+Student_name+" "+fees);

}

class checkingthiskeyword  // main method class

{

public static void main(String[] args)

{

student s1=new student(111,"aaa",1000f);   // creation of object 1 by alocating new memory

student s2=new student(222,"bbb",2000f);  // // creation of object 2 by alocating new memory

s1.display(); // calling method through object 1

s2.display(); // calling method through object 2

}

}

Output

0 Null 0.0

0 Null 0.0

Explanation of the program:- In the above example we have taken a class name student in which we declared instance variable in int, String, float data type then we have taken the parameterized constructor with three parameters in it, then we passed values in the parameterized constructor block then we we created a display() method in which we printed the rollno name of the student and fees then we have created the main method class in which we created object by allocating new memory the we have putted the value in the constructor itself and called the display() method through object which we have created. 

Reason why output is empty:- This is because in the parameterized constructor we have used the local variable same as of the instance variable that's why the complier throws the output as null and zero values to overcome this problem we have to use the reference of this keyword.

**)Overcoming this problem by using this keyword....

class student

{

int roll_no;  //  variable declaration 

String Student_name; // variable declaration

float fees; //variable declaration

student(int roll_no, String Student_name, float fees) // parameterized constructor

{

this.roll_no = roll_no;

this.Student_name = Student_name;

this.fees = fees;

}

void display()  //java method

{

System.out.println(roll_no+" "+Student_name+" "+fees);

}

class checkingthiskeyword  // main method class

{

public static void main(String[] args)

{

student s1=new student(111,"aaa",1000f);   // creation of object 1 by alocating new memory

student s2=new student(222,"bbb",2000f);  // // creation of object 2 by alocating new memory

s1.display(); // calling method through object 1

s2.display(); // calling method through object 2

}

}

Output

111 aaa 1000

222 bbb 2000

Explanation of the program:- In the above example we have taken a class name student in which we declared instance variable in int, String, float data type then we have taken the parameterized constructor with three parameters in it, then we passed values in the parameterized constructor block with the help of this keyword then we we created a display() method in which we printed the rollno name of the student and fees then we have created the main method class in which we created object by allocating new memory the we have putted the value in the constructor itself and called the display() method through object which we have created. 

Now we will see how this() constructor works with the help of one example.

class sample

{

sample()

{

System.out.println(" Hello welcome to java programming");

}

sample(int x)

{

this();

System.out.println(x);

}

}

class testingthis_constructor

{

public static void main(String[] args)

{

sample s1 = new sample(10);

}

}

Output

Hello welcome to java programming

10

Explanation of the program:- 

In the above example we have created a class name sample then we have taken  a default constructor in which we have printed a simple sentence as hello welcome to java programming, then we have taken a one parameterized constructor in which we have called default constructor through this() constructor then we have have the printing statement in which we will print the value that we have taken ass a parameter. Then we have taken the main method class in which we have created object by allocating the new memory and putted the value which we passed in the parameterized constructor.



Comments

Popular posts from this blog

Decision Making Statements in Java Programming Language.

 Their are 3 decision making statement in Java. Here where ever you find the if underlined like this( if ) then it is related to statement. 1) if statement. 2) if else statement. 3) if-else-if statement. So lets discuss one by one about each statement. 1) if statement:- if statement is used in programming for checking the given condition is true or not. If the condition in the if block is true then the block executes otherwise the programme terminates. Lets understand it by its syntax.   Here in the syntax we have condition in the if block, if the the condition which we have putted is true then the code which is written in the block will executes otherwise the programme ends their.  2) if else statement:- if else statement is used in programming for checking the given condition is true or false. If the condition is true then the if block is executed otherwise the else block is executed. Lets understand it by its syntax. Here in the syntax a condition is given in the if...

Looping Statement in Java.

  Firstly what is looping statement? So, looping statement are those statement which are executed more than one time repeatedly until the user want. There are Three looping Statement in Java. 1) for loop. 2) while loop. 3) do while loop. Lets understand them by their syntax. 1) for loop for loop is a control flow statement for specifying iteration, which allows code to be executed repeatedly until the user wants. Lets understand by one example:-   Here in the example we have initiated a class with name for_loop then we have main method in the main method we have for loop in that for loop we have statement in which i is initialize to 0 and i is less than 5 and i++ for incrementing the value of i. And in that for loop we have given a statement to print the value of i. Then we have completed the blocks and generated the output. 2) while loop while loop is also a control flow statement that allows code to be executed repeatedly based on the given Boolean condition. Lets understand...

What is Procedural Programming Language(PPL)

  Procedural Programming Language. Procedural Programming might be the primary programming worldview that another engineer will learn. Essentially, the procedural code is the one that straightforwardly trains a gadget on the best way to complete an errand in consistent advances. This worldview utilizes a direct top-down methodology and treats information and techniques as two distinct elements. In view of the idea of a methodology call, Procedural Programming isolates the program into strategies, which are otherwise called schedules or capacities, just containing a progression of steps to be done.  Basically, Procedural Programming includes recording a rundown of directions to mention to the PC what it ought to do bit by bit to complete the job needing to be done.  Key Highlights of Procedural Programming  The vital highlights of procedural writing computer programs are given underneath:  1)Predefined capacities: A predefined work is normally a guidance distingu...