Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using this to call a instance variable?

Tags:

java

Should you use "this.variablename" or just "variablename" to reference a member variable in a method?

like image 589
ajsie Avatar asked Jan 13 '10 03:01

ajsie


People also ask

How do you call an instance variable?

Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are given accessibility), they should be called using the fully qualified name. ObjectReference. VariableName.

Does this refer to instance variable?

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.

How do you call an instance variable in Python?

There are two ways to access the instance variable of class: Within the class in instance method by using the object reference ( self ) Using getattr() method.


1 Answers

You can use whichever you want in most cases. If your method parameter or local variable has the same name then you'll need to use this to distinguish the instance variable. Be consistent!

like image 68
Joe Phillips Avatar answered Sep 19 '22 18:09

Joe Phillips