25
loading...
This website collects cookies to deliver better user experience
this.variable = variable;
Class {
int x;
Class () {
this(3);
}
Class(int x) {
this.x = x;
}
}
public Class method(){
class.name = “Fred”;
class.age = 30;
return this;
}
void displayBikeModel(Bike bike){
System.out.println(bike.modelName);
}
void printBikeType(){
displayBikeModel(this);
}
void display(){
this.print();
}
void print(){
System.out.println(“Hello World”);
}
// Java code for using this as an argument in constructor
// call
// Class with object of Class B as its data member
class A
{
B obj;
// Parameterized constructor with object of B
// as a parameter
A(B obj)
{
this.obj = obj;
// calling display method of class B
obj.display();
}
}
class B
{
int x = 5;
// Default Constructor that create a object of A
// with passing this as an argument in the
// constructor
B()
{
A obj = new A(this);
}
// method to show value of x
void display()
{
System.out.println("Value of x in Class B : " + x);
}
public static void main(String[] args) {
B obj = new B();
}
}
'this' reference in Java. GeeksforGeeks. (2021, April 23). https://www.geeksforgeeks.org/this-reference-in-java/.
Using the this Keyword. Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects). (n.d.). https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html.
Kasyap, K. (2019, July 2). Can we return this keyword from a method in java? https://www.tutorialspoint.com/can-we-return-this-keyword-from-a-method-in-java.