Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use "this" keyword when I want to refer to instance variables within a method?

My teacher says that when I try to access an instance variable within a method I should always use the this keyword, otherwise I would perform a double search. A local scope search and then an instance scope search.

Example:

public class Test(){     int cont=0;     public void Method(){         System.out.println(cont);//Should I use This.cont instead?     } } 

I hope he is wrong, but I can't find any argument.

like image 291
Juan Herrera Avatar asked Apr 05 '13 03:04

Juan Herrera


People also ask

Can this keyword be used in instance method?

The "this" keyword in Java is used as a reference to the current object, within an instance method or a constructor. Yes, you can call methods using it.

Which keyword is used with instance variables?

Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed. Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.

Can we use instance variable in method?

No. 1. Variables declared within a method are local variables. An instance variable is declared inside a class but outside of any method or block.

Why do we need to use the this keyword in our instance methods?

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this .

Why do we use same name for instance variables and parameters?

So we use same name for instance variables and parameters in real time, and always use this keyword. You may invoke the method of the current class by using the this keyword. If you don't use the this keyword, compiler automatically adds this keyword while invoking the method. Let's see the example

Can I use'rollno+'keyword for instance variables?

If local variables (formal arguments) and instance variables are different, there is no need to use this keyword like in the following program: void display () {System.out.println (rollno+" "+name+" "+fee);}

What is the use of this keyword in Java?

There can be a lot of usage of Java this keyword. In Java, this is a reference variable that refers to the current object. Here is given the 6 usage of java this keyword. this can be used to refer current class instance variable. this () can be used to invoke current class constructor. this can be passed as an argument in the method call.

What does it mean when a variable has the same name?

It means this is nothing but the reference to the current object. There are various situations where this keyword is commonly used. In Java, it is not allowed to declare two or more variables having the same name inside a scope (class scope or method scope). However, instance variables and parameters may have the same name. For example,


1 Answers

No, only use this when you have a name conflict such as when a method parameter has the same name as an instance field that it is setting.

It can be used at other times, but many of us feel that it simply adds unnecessary verbiage to the code.

like image 111
Hovercraft Full Of Eels Avatar answered Oct 17 '22 07:10

Hovercraft Full Of Eels