this keyword in Java is a special keyword which can be used to represent current object or instance of any class in Java. “this” keyword can also call constructor of same class in Java and used to call overloaded constructor. In this article we will see how to use this keyword in Java and different examples. this sometime also associate with super keyword which is used to denote instance of super class in Java and can be used to call overloaded constructor in Java.
6 usage of this keyword
1. to refer current class instance variable
Program
//Java code for using 'this' keyword to
//refer current class instance variables
class demo
{
int a;
int b;
// parameterized constructor
demo(int a, int b)
{
this.a = a;
this.b = b;
}
void display()
{
//displays the value of variables a and b
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
public class Main
{
public static void main(String[] args)
{
demo object = new demo(10, 20);
object.display();
}
}
Output
a = 10
b = 20
Explanation
The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.
In the above example, parameters (formal arguments) and instance variables are same. So, we are using this keyword to distinguish local variable and instance variable.If local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program.It is better approach to use meaningful names for variables. So we use same name for instance variables and parameters in real time, and always use this keyword.
2. to invoke current class method
Program
// this keyword in class method
class demo
{
void display() // 3
{
// calling fuction show()
this.show(); // 4
System.out.println("We are inside display function"); // 7
}
void show() // 5
{
System.out.println("We are inside show funcion"); // 6
}
}
public class Main
{
public static void main(String args[])
{
demo t1 = new demo(); // 1
t1.display(); // 2
}
}
Output
We are inside show funcion
We are inside display function
3. to invoke current class constructor
Program
class demo
{
int a;
int b;
//2. Default constructor
demo()
{
//3. calling parameterized constructor of this class
this(10, 20);
//5. back in default constructor
System.out.println("We are inside default constructor \n");
}
//4. Parameterized constructor
demo(int a, int b)
{
this.a = a;
this.b = b;
System.out.println("We are inside parameterized constructor");
}
}
public class Main
{
public static void main(String[] args)
{
//1. calls default constructor
demo object = new demo();
}
}
Output
We are inside parameterized constructor
We are inside default constructor
4. as an argument in the method call
Program
// this keyword in method parameter
class demo
{
int a;
int b;
// Default constructor
demo()
{
a = 10;
b = 20;
}
// Method that receives 'this' keyword as parameter
void display(demo obj) // 5
{
System.out.println("a = " + a + " b = " + b); // 6
}
// Method that returns current class instance
void get() // 3
{
display(this); // 4
}
}
public class Main
{
public static void main(String[] args)
{
demo object = new demo(); // 1
object.get(); // 2
}
}
Output
a = 10 b = 20
5. as argument in the constructor call
Program
class A
{
B obj;
// Parameterized constructor with object of B as a parameter
A(B obj) // 4
{
this.obj = obj; // 5
// calling display method of class B
obj.display(); // 6
}
}
class B
{
int x = 5;
// Default Contructor that create a object of A with passing this as an argument in the constructor
B() // 2
{
A obj = new A(this); // 3
}
// method to show value of x
void display() // 7
{
System.out.println("Value of x in Class B is " + x); // 8
}
public static void main(String[] args) {
B obj = new B();
}
}
public class Main
{
public static void main(String[] args)
{
B obj = new B(); // 1
}
}
Output
Value of x in Class B is 5
6. to return the current class instance from the method
Program
//to return the current class instance
class demo
{
int a;
int b;
//Default constructor
demo()
{
a = 10;
b = 20;
}
//Method that returns current class instance
// 3
demo get()
{
// 4. returns the instance of class demo
return this;
}
//Displaying value of variables a and b
void display() // 6
{
System.out.println("a = " + a + " b = " + b); // 7
}
}
public class Main
{
public static void main(String[] args)
{
//1. create object of class demo
demo object = new demo();
object.get().display(); // 2 // 5
}
}
1. Download JDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html 2. Install the JDK. 3. Go to path C:\Program Files (x86)\Java\jdk1.8.0_131\bin in your PC and copy it. 4. Now go to control panel 5. Click on system 6. Click on advance system settings 7. Click on environment variables 8. Click on new and name it as path and copy path which is in your pc as shown above in red colour 9. Click on OK 10. Now open CMD and type javac and press enter if it shows messy long list then congratulation java is installed in your PC. 11. Now go to next article for Hell...
Methods appear inside class bodies. They contain local variable declarations and other Java statements that are executed when the method is invoked. Methods may return a value to the caller. They always specify a return type, which can be a primitive type, a reference type, or the type void , which indicates no returned value. Methods may take arguments, which are values supplied by the caller of the method. Lets see example: Program class multiply { int num1; int num2; int mul(int x,int y) { int result; num1 = x; num2 = y; result = num1*num2; return result; } } public class Main { public static void main (String[] args) { multiply m = new multiply(); System.out.println("multiplication of 3 and 5 is " + m.mul(3,5)); } } Run Output multiplication of 3 and 5 is 15 Explanation In this example, the class multiply defines a method, mul() , that takes as argu...
C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework. You can use C# to create Windows client applications, XML Web services, distributed components, client-server applications, database applications, and much, much more. Visual C# provides an advanced code editor, convenient user interface designers, integrated debugger, and many other tools to make it easier to develop applications based on the C# language and the .NET Framework. C# Language C# syntax is highly expressive, yet it is also simple and easy to learn. The curly-brace syntax of C# will be instantly recognizable to anyone familiar with C, C++ or Java. Developers who know any of these languages are typically able to begin to work productively in C# within a very short time. C# syntax simplifies many of the complexities of C++ and provides powerful features such as nullable value types, enumerations, d...
Comments
Post a Comment