this is a one of the keyword amongst the other in java, and java objects includes a data member this thats actually a reference to the current object. this keyword is useful if you need to refer to the current object - e.g. when you want to pass the current object to the method or constructor of class.
to understand more look at below code snippet in which a class Number has method printNumber() which prints the value stored in class variable mValue, with the help of NumberPrinter class, which takes the reference of Number class in its constructor.
printNumber() method creates the instance of NumberPrinter and pass its own reference in the form of this.
Output:
to understand more look at below code snippet in which a class Number has method printNumber() which prints the value stored in class variable mValue, with the help of NumberPrinter class, which takes the reference of Number class in its constructor.
printNumber() method creates the instance of NumberPrinter and pass its own reference in the form of this.
Output:
Comments
Post a Comment