The keyword 'this' in Java

Han sang yeob·2021년 4월 26일
0

Java basic

목록 보기
1/1

Sometimes, in a Java class, it is convenient to reference the current instance of that class. Java provides a keyword, called this, for such a reference. Reference this is useful, for example, if we would like to pass the current object as a parameter to some method. Another application of this is to reference a field inside the current obbject that has a name clash with a variable defined in the current block.


public class Main {
    public Integer tabacoo = 4; // instance variable

    public void malboro(){
        Integer tabacoo = 40;
        System.out.println("The tabaco local variable =" + tabacoo);
        System.out.println("The tabaco field =" + this.tabacoo);
    }

    public static void main(String[] args) {

        Main m = new Main();
        m.malboro();
    }
}

Output

The tabaco local variable =40
The tabaco field =4
profile
Learning IT in Vietnam

0개의 댓글