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
}
}
Like any programming language, C# defines keywords for fundamental data types, which are used to represent local variables, class data member variables, method return values, and parameters. Unlike other programming languages, however, these keywords are much more than simple compiler- recognized tokens. Rather, the C# data type keywords are actually shorthand notations for full-blown types in the System namespace. Table lists each system data type, its range, the corresponding C# keyword, and the type’s compliance with the common language specification (CLS). C# keyword System type Range Meaning CLS complaint? bool System.Boolean true or false Represents truth or falsity Yes sbyte System.SByte -128 to 127 signed 8-bit number No byte System.Byte 0 to 255 Unsigned 8-bit ...
In this article we will see members of C# datatypes. Members of datatype Members of numerical datatype Experimenting with the intrinsic C# data types, understand that the numerical types of .NET support MaxValue and MinValue properties that provide information regarding the range a given type can store. In addition to the MinValue/MaxValue properties, a given numerical system type may define further useful members. For example, the System.Double type allows you to obtain the values for epsilon and infinity which might be of interest to those of you with a mathematical flare. Lets understand that by a simple program: Program using System; public class ConsoleApp { public static void Main(string[] args) { Console.WriteLine("Members of numerical datatype:"); Console.WriteLine("Max value of Int: {0}",int.MaxValue); Console.WriteLine("Min value of Int: {0}",int.MinValue); Console.WriteLine(...
Comments
Post a Comment