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();
}
}