Java Program using this keyword as return statement


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


First msg() { return this; }


Example: How to implement this keyword as return statement in Java

Run it Now
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