Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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
Comment

this keyword in java

/**
 * This keyword,
 * this keyword is a reference variable that refers to the current class
 * instance.
 */
class Student {
    int id;
    String name;

    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    void display() {
        System.out.println(this.id);// both way can call
        System.out.println(this.name);
        System.out.println(id);
        System.out.println(name);
    }
}

public class This {
    public static void main(String[] args) {
        Student student = new Student(10, "Mike");
        student.display();
    }
}
Comment

this Keyword Java

class Main {
    int instVar;

    Main(int instVar){
        this.instVar = instVar;
        System.out.println("this reference = " + this);
    }

    public static void main(String[] args) {
        Main obj = new Main(8);
        System.out.println("object reference = " + obj);
    }
}
Comment

how to use the this keyword in java

class Other{
    public double num;
    public Other(int num){
        this.num = num;
        System.out.println(num);
      	//print 5 to the console
    }
}

class scratch{
    public static void main(String[] args) {
        Other method = new Other(5);
        System.out.println(method.num);
      	//prints 5.0 to the console
    }
}
Comment

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
  }
}

Comment

Java this Keyword

public class Main {
  int x;

  // Constructor with a parameter
  public Main(int x) {
    this.x = x;
  }

  // Call the constructor
  public static void main(String[] args) {
    Main myObj = new Main(5);
    System.out.println("Value of x = " + myObj.x);
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to import class from another file in java 
Java :: java generate uuid 
Java :: switch case in android studio - java 
Java :: statusbar text color android 
Java :: blurview android github 
Java :: Java push() Method 
Java :: how to center a window in java 
Java :: spring db properties 
Java :: java generate random integer in range 
Java :: Java Writer Using FileWriter 
Java :: declare variables java 
Java :: getcolor deprecated android 
Java :: has been compiled by a more recent version of the Java Runtime (class file version ), this version of the Java Runtime only recognizes class file versions up to 
Java :: Could not determine java version from 
Java :: load contents of file into string java 
Java :: java read a line of input 
Java :: get index of element java 
Java :: java list select field 
Java :: jtable font size 
Java :: set spring context 
Java :: assert multiple junit 
Java :: how to break switch case in java 
Java :: chenge font android studio 
Java :: how to turna date into a LocalDateTime java 
Java :: class declaration in java 
Java :: current time stamp android java 
Java :: java filter array by even numbers 
Java :: string.indexof java 
Java :: get index of element in array java 
Java :: how to create an array without knowing the size java 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =