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

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

PREVIOUS NEXT
Code Example
Java :: how to output sum of even numbers in java between two user values 
Java :: create array of particular length in java 
Java :: java generate uuid 
Java :: armstrong number in java 
Java :: convert fileinputstream to string java 
Java :: how generate a random number in java between 3 and 5 
Java :: java binart tree print 
Java :: java string array to arraylist 
Java :: how to delete character in string java 
Java :: android sha1 key 
Java :: camera permission in android 
Java :: how to reverse a string in java without function 
Java :: java regex case insensitive 
Java :: make textview invisible android 
Java :: mapping over a list of promises in javaascript 
Java :: calculate time java 
Java :: indext of minimum element in an array in java 
Java :: android menu change text color 
Java :: java vector push_back 
Java :: use regex in if statement java 
Java :: System.out.println("Hello world") 
Java :: how to multiply bigdecimals 
Java :: Error: Could not find or load main class javafx.controls,javafx.fxml Caused by: java.lang.ClassNotFoundException: javafx.controls,javafx.fxml 
Java :: how to use do while loop in java 
Java :: android studio get id name from view 
Java :: arraylist index java 
Java :: java netbeans textfield only numbers 
Java :: get input in java 
Java :: hashmap in java 
Java :: how to find the largest number in java 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =