Java Program using this keyword as a return statement


In this program, You will learn how to implement this keyword as a return statement in java.


First msg() { 
   return this; 
}

Example: How to implement this keyword as a return statement in java.

class First {
    First msg() {
        System.out.println(this);
        return this;
    }
}

class Main {

    First t = new First();

    First print() {
        System.out.println(t);
        return t;
    }

    public static void main(String args[]) {
        Main tt = new Main();
        First ft = tt.print();
        System.out.println(ft);
        ft.msg();
    }
}

Output:

First@4aa298b7
First@4aa298b7
First@4aa298b7