Is using the keyword this
optional in Java? Or do I have to use it? And when it is not optional?
In the following code it does not affect my application, regardless of how many instances of Employee
I made.
The print
method prints Employee details without anything wrong even if details do not match.
public class Employee {
String name;
int Salary;
int pension;
String workPlace;
String teleNo;
int age;
void printDetails(){
System.out.println("Name is : "+this.name );
System.out.println("age is : "+this.age );
System.out.println("WorkPlace is : "+this.workPlace );
System.out.println("Salary is : "+Salary );
System.out.println("Pension is : "+this.pension );
System.out.println("Telephone No. is : "+this.teleNo );
System.out.println("age is : "+Integer.toString(age) );
}
}
public class Main extends Employee {
/**
* @param args
*/
public static void main(String[] args) {
Employee obj=new Employee();
obj.age=25;
obj.name="Yasser";
obj.pension=100_000;
obj.teleNo="xxx_xxxx";
obj.workPlace="Egypt";
obj.Salary=1000000;
obj.printDetails();
Employee obj1=new Employee();
obj1.age=29;
obj1.name="asser";
obj1.pension=100_000;
obj1.teleNo="xxx_xxxx";
obj1.workPlace="rgypt";
obj1.Salary=2000000;
obj1.printDetails();
}
}
Definition and Usage The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter). If you omit the keyword in the example above, the output would be "0" instead of "5".
The this is a keyword in Java which is used as a reference to the object of the current class, with in an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables and methods.
The main purpose of using this keyword in Java is to remove the confusion between class attributes and parameters that have same names. Following are various uses of 'this' keyword in Java: It can be used to refer instance variable of current class. It can be used to invoke or initiate current class constructor.
The correct answer to the question “What is not the use of 'this' keyword in Java” is, option (d). Passing itself to the method of the same class. This is one of the most important keywords in Java and is used to distinguish between local variables and variables that are passed in the methods as parameters.
It's always optional unless you are referencing a field which has the same name as a local variable. For example:
class Sample
{
int value;
void method(int value)
{
this.value = value; //this is required
}
}
If this is not the case, then using this
behaves identically to referencing the variable directly.
this
in Java is optional, unless you have a local variable or parameter of the same name, in which case the variable name will be referring to the latter.
public class Employee {
private String name = "one";
public void printDetails() {
String name = "two";
System.out.println(this.name); // "one"
System.out.println(name); // "two"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With