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

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.