Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java using this word is optional or not? [duplicate]

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();
    }
}
like image 766
ياسر محمد Avatar asked Mar 09 '15 15:03

ياسر محمد


People also ask

Is it necessary to use this keyword in Java?

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".

What is this () in Java?

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.

Do you need to use this in Java class?

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.

Which is not the use of this keyword in Java?

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.


2 Answers

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.

like image 74
Kon Avatar answered Oct 17 '22 04:10

Kon


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"
    }
}
like image 4
Jaroslaw Pawlak Avatar answered Oct 17 '22 05:10

Jaroslaw Pawlak