Answers for "!this java"

7

this in java

this() : used for calling the constructor . 
  we can only use it in the constructor
this. : used for calling the instance variables 
we can use in any object instances
Posted by: Guest on May-28-2021
1

this in java

public class MyThisTest {
  private int a;

  public MyThisTest() {
    this(42); // calls the other constructor
  }

  public MyThisTest(int a) {
    this.a = a; // assigns the value of the parameter a to the field of the same name
  }

  public void frobnicate() {
    int a = 1;

    System.out.println(a); // refers to the local variable a
    System.out.println(this.a); // refers to the field a
    System.out.println(this); // refers to this entire object
  }

  public String toString() {
    return "MyThisTest a=" + a; // refers to the field a
  }
}
Posted by: Guest on April-04-2022

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language