Solved

toString method

Posted on 2014-02-28
2
253 Views
Last Modified: 2014-02-28
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;
}
}
0
Comment
Question by:zizi21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 39896336
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
0
 

Author Closing Comment

by:zizi21
ID: 39896348
Thanks !
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question