toString method

zizi21
zizi21 used Ask the Experts™
on
Hi,

In the java classes, is it common to write this.variable in the toString method when you are overriding it. Why is "this" written? You are able to access the variable without this.
i.e
public class test{
private String a;

@Override
      public String toString() {
return this.a;
}
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Top Expert 2011
Commented:
You only really need to use the 'this' keyword if the variable is shadowed by a method or constructor parameter see :

http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

Author

Commented:
Thanks !

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial