/**
* 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();
}
}
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);
}
}
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
}
}
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);
}
}