this Keyword In Java ProgrammingsteemCreated with Sketch.

in #java4 years ago

The following program source shows how this keyword is used to refer to instance variable of the class so as to avoid the confusion between the local variable and the instance variable.

public class ThisKeyword {

    public static void main(String[] args) {
        
        IntSum obj=new IntSum(5,6);
        obj.displaysum();

    }
}

class IntSum{
    private int a;
    private int b;
    private int sum;
    
    public IntSum(int a, int b) {
        this.a=a;
        this.b=b;   
        sum=a+b;
    }
        
    public void displaysum() {
        System.out.println("The sum of the number is " + sum);
    }
}

The output of this code is as below:


Screenshot_1.png

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.033
BTC 62559.43
ETH 3092.10
USDT 1.00
SBD 3.86