In: Computer Science
Create a class that has a method that uses the ‘this’ keyword as the return statement. This method (called increment( ) in the class that you have just created ) increments the private integer field (private i ) in the class. In the main ( ) method, if you increment (using the ‘this’ with increment() call) four times, you should see the following display:
i is = 4
Thank You
public class Main
{
private int i;
Main()
{
i=0;
}
public void increment()
{
this.i = this.i+1;
}
public int get()
{
return this.i;
}
public static void main(String[] args) {
Main n = new Main();
n.increment();
n.increment();
n.increment();
n.increment();//incrementing 4 times
System.out.println("i is
="+n.get()+"\nThank You");
}
}
output:
i is =4
Thank You