Should you use "this.variablename" or just "variablename" to reference a member variable in a method?
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.
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.
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.
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!
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