(compsci) when do you use this.instanceVariable?

<p>when do you use this.instanceVariable?</p>

<p>i don't see a single instance in which one can use it.</p>

<p>can you give me an example?</p>

<p>Also:</p>

<p>I don't under stand when we need to use the return statement.
These are in the same class method as the main method. (same file)</p>

<p>i know we need "return someValue; " when we have specified some type of return value in method declaration. but why not just have the return value as void and put System.out.print(someValue); ?</p>

<p>for instance...</p>

<p>String name = "someName"; </p>

<pre><code> public static String name1()
{
return name;
}
public static void name1()
{

System.out.println(name);
}
</code></pre>

<p>name1 and name2 methods both print out "someName".</p>

<p>(disclaimer: i've taken less than a year of java so if someone corrects me, i'd be more willing to believe them.)</p>

<p>return just returns name from the method to main. it never says it's outputed. once it's returned to the main, name would still need a SOP to be outputted.</p>

<p>don't believe that's right. </p>

<p>you can call the name method by typing "name1(name)" in the main method.
it would return name and S.O.P would not be needed. </p>

<p>correction: i should have added "String name" as a parameter to "name1()'s" method declaration.</p>

<p>the return allows you to store name as a variable in the main. So.....</p>

<p>public static void main(String []args)
{
String name = name1("Joe");
System.out.println(name);
}</p>

<p>The usefulness of the return is in the ability to store name as a variable in main.</p>

<p>yes, it does return name, but it doesn't output it. i made a small program with both methods and only the one where you use SOP actually outputs it. i suggest trying it and seeing what happens. making programs and compiling and running them is the best way to learn how they work.</p>

<p>return is not the same as printing. Printing is a much more limited concept. When you get into GUIs, do you still think print has any value?</p>

<p>You usually use this.something when you are trying to call a method inside of an object class.</p>

<p>Public Cat()
{
this(20);
}
Public Cat(int pounds)
{
weight = pounds;
}</p>

<p>bemyparaguay, oops, you're right.</p>

<p>i stil don't get "this.something"</p>

<p>dlevit, that's calling the class constructor from within the object. It sounds interesting...but is it legal?</p>

<p>tigeruppercut, like dlevit said, this.something() calls a method inside the current object's class. It is mostly used to call static methods, I think. Something with them not able to be called directly or another...I don't remember the details.
this.instanceVariable would be invoking one of the object's instance variables. It is particularly useful if there exists a variable with the same name as the instance variable in the method. By using the variable name directly, the variable inside the method would be invoked. But by using this.variableName, the object's instance variable by that name, instead of the variable in the method, would be called.</p>

<p>gotcha, dualityim.</p>