What is the meaning of “this” in Java?

后端 未结 21 2961
走了就别回头了
走了就别回头了 2020-11-21 05:41

Normally, I use this in constructors only.

I understand that it is used to identify the parameter variable (by using this.something), if i

21条回答
  •  半阙折子戏
    2020-11-21 06:21

    The this Keyword is used to refer the current variable of a block, for example consider the below code(Just a exampple, so dont expect the standard JAVA Code):

    Public class test{
    
    test(int a) {
    this.a=a;
    }
    
    Void print(){
    System.out.println(a);
    }
    
       Public static void main(String args[]){
        test s=new test(2);
        s.print();
     }
    }
    

    Thats it. the Output will be "2". If We not used the this keyword, then the output will be : 0

提交回复
热议问题