Java program for returning the current class object with this keyword

 import java.util.*;

public class Return

{

 private String name;

 private int age;

 public Return SetValues()

 {

  Scanner sc = new Scanner(System.in);

  System.out.println("Enter the name of the student");

  String name = sc.nextLine();

  System.out.println("Enter the age of the student");

  int age = sc.nextInt();

  this.name = name;

  this.age = age;

  return this;

 }

 public void display()

 {

  System.out.println("name : "+name);

  System.out.println("age : "+age);

 }

 public static void main(String ar[])

 {

  Return rohit = new Return();

  Return mohit;

  mohit = rohit.SetValues();

  mohit.display();

 } 

}



OUTPUT





Comments