Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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

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 :: Java @Repeatable 
Java :: java.sql.SQLException: Invalid column index 
Java :: super class and concrete class in java 
Java :: java get if contains else 0 
Java :: convert from java to kotlin online 
Java :: site:stackoverflow.com List is abstract; cannot be instantiated public List<Integer result = new List<(); 
Java :: How to code the Fibonacci Sequence using simple iterative loops java 
Java :: remove minimum element from stack java 
Java :: Java search() Method 
Java :: api to accept a csv file spring boot 
Java :: io fole scanner object syntax 
Java :: java method 
Java :: open youtube by default in full screen pragmatically in android 
Java :: Java Constructor invocations 
Java :: Returning methods 
Java :: how to write to a txt file in java in the end 
Java :: integer to roman 
Java :: java static inner class 
Java :: longadder 
Java :: arrotondare un numero a 2 cifre dopo la virgola java 
Java :: Pre Render View 
Java :: java india 
Java :: Array first Occurence 
Java :: text with seek bar in android 
Java :: call to jdbc template each class not by super 
Java :: produces 
Java :: flutter calculate sum in a list 
Java :: check if object is a string java 
Java :: java window always on top 
Java :: ratespiel java 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =