Java find output programs (this Keyword) | set 1

Find the output of Java programs | this Keyword | Set 1: Enhance the knowledge of Java this Keyword concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on January 30, 2021

Question 1:

class Sample {
  private int val1;
  private int val2;

  public Sample(int val1, int val2) {
    this.val1 = val1;
    this.val2 = val2;
  }

  public void display() {
    System.out.println("Val1: " + val1);
    System.out.println("Val2: " + val2);
  }

}

public class Main {
  public static void main(String[] args) {
    Sample S = new Sample(10, 20);

    S.display();
  }
}

Output:

Val1: 10
Val2: 20

Explanation:

In the above program, we created a class Sample that contains data members val1 and val2, and we defined a constructor and method display() inside the Sample class.

public Sample(int val1, int val2)
{
        this.val1 = val1;
        this.val2 = val2;
}

In the constructor of Sample class, here val1 and val2 are data members and also a parameter of the constructor. Here we used "this" object, which is used to differentiate data members and parameters. "this" object is used with data members and we can use local parameters directly without using "this" object.

Question 2:

class Sample {
  static int val1;
  static int val2;

  public Sample(int val1, int val2) {
    this.val1 = val1;
    this.val2 = val2;
  }

  public void display() {
    System.out.println("Val1: " + val1);
    System.out.println("Val2: " + val2);
  }

}

public class Main {
  public static void main(String[] args) {
    Sample S = new Sample(50, 80);

    S.display();
  }
}

Output:

Val1: 50
Val2: 80

Explanation:

In the above program, we created a class Sample that contains static data members val1 and val2, and we defined a constructor and method display() inside the Sample class.

public Sample(int val1, int val2)
{
        this.val1 = val1;
        this.val2 = val2;
}

In the constructor of Sample class, here val1 and val2 are data members and also a parameter of the constructor. Here, we used "this" object, which is used to differentiate data members and parameters. "this" object is used with data members and we can use local parameters directly without using "this" object.

Question 3:

class Sample {
  void fun1() {
    System.out.println("Method fun1() called");
  }
  void fun2() {
    System.out.println("Method fun2() called");
  }
  void fun3() {
    System.out.println("Method fun3() called");
  }
}

public class Main {
  public static void main(String[] args) {
    Sample S = new Sample();

    S.fun1().fun2().fun3();
  }
}

Output:

Main.java:17: error: void cannot be dereferenced
    S.fun1().fun2().fun3();
            ^
1 error

Explanation:

The above program will generate a compile-time error because we cannot methods of a class like this,

S.fun1().fun2().fun3();

To call methods like the above statements we need to implement a cascaded method call using "this" object.

Question 4:

class Sample {
  Sample fun1() {
    System.out.println("Method fun1() called");
    return this;
  }
  Sample fun2() {
    System.out.println("Method fun2() called");
    return this;
  }
  Sample fun3() {
    System.out.println("Method fun3() called");
    return this;
  }
}

public class Main {
  public static void main(String[] args) {
    Sample S = new Sample();

    S.fun1().fun2().fun3();
  }
}

Output:

Method fun1() called
Method fun2() called
Method fun3() called

Explanation:

In the above program, we created a Sample class that contains three method fun1(), fun2(), and fun3(). All three methods return the object of the Sample class using "this" object. Here we implemented a cascaded method call,

S.fun1().fun2().fun3();

As we know that every method call requires an object, in the above method every method returns an object that's why we are able to call methods in a cascaded manner.

Question 5:

class Sample {
  static Sample fun1() {
    System.out.println("Method fun1() called");
    return this;
  }
  Sample fun2() {
    System.out.println("Method fun2() called");
    return this;
  }
  Sample fun3() {
    System.out.println("Method fun3() called");
    return this;
  }
}

public class Main {
  public static void main(String[] args) {
    Sample S = new Sample();

    S.fun1().fun2().fun3();
  }
}

Output:

Main.java:4: error: non-static variable this cannot be referenced from a static context
    return this;
           ^
1 error

Explanation:

The above program will generate a syntax error because fun1() is a static method, and a static method cannot return reference of the object.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.