BLUEJ THEORY 3

this keyword is used to refer to the object on which a method was invoked.
Take a look at the following code which contains a single instance variable x and a method setX().
1public class MyNumber {
2 
3    int x;
4 
5    public void setX() {
6        this.x = 3;
7    }
8}
Now, to understand what this.x means, we use the following code
1public class Test {
2 
3    public static voidmain(String[] args) {
4        MyNumber myNumber1 =new MyNumber();
5        myNumber1.setX();
6    }
7}
The method setX() was called on the object myNumber1. Within setX(), the statement this.x=3 will be executed. As we said in the beginning of this article, ‘this refers to the object on which a method was invoked’. Here, we have invoked the method setX() on the object myNumber1. So, this will refer to the object myNumber1. So,
this.x = 3 ;
will be interpreted as
myNumber1.x = 3
which will set x to 3.
We could written the above program without using this keyword in the following way.
1public class MyNumber {
2 
3    int x;
4 
5    public void setX() {
6        x = 3;
7    }
8}
The output will still be the same.
Now, you might ask what is the use of this keyword if we can achieve the same thing without using it.
Consider the following example:
1public class MyNumber {
2 
3    int x = 3;
4 
5    public void printX() {
6        int x = 4;
7        System.out.printn(x);
8    }
9}
The method printX() will print 4 and not 3. This is because when we declare a variable in a method with the same name as that of another instance variable, then when we refer to that variable within the method, we get access to the variable defined in the method and not the instance variable of the class. This is illustrated in the following diagram:
If we want to access the instance variable, we do it with this.x.
1public class MyNumber {
2 
3    int x = 3;
4 
5    public void setX() {
6        x = 4;
7        System.out.printn(x);
8        System.out.printn(this.x);
9    }
10}
The above program will print,
14
23
To see why the second line printed 3, let us use the first line in this article – ‘this refers to the object on which the method was invoked’.
1MyNumber myNumber1 = newMyNumber();
2myNumber1.printX();
The method printX() was invoked on the object MyNumber1. So, this will refer to myNumber1 and this.x will be myNumber1.x which is 3.
Normally, we use the ‘this’ keyword in constructors and set methods so that we can use the same name for instance variables and formal parameters.
Here is an example program with the keyword this used in a constructor and two set methods.
1public class Student {
2 
3    int rollNumber;
4    int marks;
5 
6    public Student(introllNumber, int marks) {
7        this.rollNumber = rollNumber;
8        this.marks = marks;
9    }
10 
11    public voidsetRollNumber(int rollNumber) {
12        this.rollNumber = rollNumber;
13    }
14 
15    public void setMarks(intmarks) {
16        this.marks = marks;
17    }
18}

No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...