We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › use of objects returned from a method
Page Index Toggle Pages: 1
use of objects returned from a method (Read 217 times)
use of objects returned from a method
Jan 29th, 2009, 6:41pm
 
Hey,

I wasn't sure how to word this question or my google searching probably would have given me better results.

I've noticed that in processing, if i have a method that returns an object, I can't use that object right as it's returned, I have to save a copy of it and then check that copy.

I thought i recalled being able to do something like the following in java:

class one
{
 someObject theObject;
 someObject returnObject()
 {
   return theObject;
 }
}
one One = new one();



say class someObject contains a variable called x, I can't seem to do the following:


if((One.returnObject()).x>5)
{
  // do something
}

I would have to do this:
someObject SomeObjectCopy = One.returnObject();
if(SomeObjectCopy.x>5)
{
  // do something
}


is this a limitation of java 1.4 syntax? or am i nuts?

thanks.
Re: use of objects returned from a method
Reply #1 - Jan 29th, 2009, 6:52pm
 
You are nuts! Wink

Well, no, not really.
Sometime, I prefer to use the intermediary variable. But it is rarely, if ever, needed.

Let's demonstrate that re-using your example, only using a more conventional notation (we are used to see class names with initial cap and object instances with initial lowercase letter).

Code:
class Foo
{
Bar bar;
Foo()
{
bar = new Bar(5);
}
Bar getBar()
{
return bar;
}
}

class Bar
{
int x;
Bar(int i) { x = i; }
}

void setup()
{
Foo foo = new Foo();
println(foo.getBar().x);
}

At worse, in some case (Collections pre-Java 1.5), you need to cast the returned object to a given type before using it:

Bar b = (Bar) arrayList.get(7);
b.doSmt();

but you can even do (at the expense of some readability):

((Bar) arrayList.get(7)).doSmt();
Page Index Toggle Pages: 1