<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>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>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>