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
}
}
Environment class allows you to obtain a number of details regarding the operating system currently hosting your .NET application using various static members. To illustrate the usefulness of System.Environment, update your Main() method to call a helper method named EnvironmentDetails() . Properties Meaning ExitCode Gets or sets the exit code for the application Is64BitOperatingSystem Returns a bool to represent whether the host machine is running a 64-bit OS MachineName Gets the name of the current machine NewLine Gets the newline symbol for the current environment SystemDirectory Returns the full path to the system directory UserName Returns the name of the user that started this application Version Returns a Version object that represents the version of the .NET platform Create EnvironmentDetails() method in class under Main() method and call this method in Main() method. Program
It is true that a console user interface (CUI) may not be as enticing as a graphical user interface (GUI) or web application, restricting the early examples to console programs will allow you to keep focused on the syntax of C# and the core aspects of the .NET platform, rather than dealing with the complexities of building desktop GUIs or web sites.As its name implies, the Console class encapsulates input, output, and error-stream manipulations for console-based applications. As you can see in below table the Console class does provide some members that can spice up a simple command-line application, such as the ability to change background and foreground colors and issue beep noises in a variety of frequencies. Members of System.Console Member Meaning Beep() This method forces the console to emit a beep of a specified frequency and duration. BackgroundColor,ForegroundColor These properties
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
Comments
Post a Comment