• Post Reply Bookmark Topic Watch Topic
  • New Topic

IF statements  RSS feed

 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone please help me refresh my memory as to whether this is possible in Java.... or am I thinking of some other language..

With the humble IF statement is it possible to call a method (with return value) and perform the condition test on the result of that method additionally being able to access the return value in the IF block...

E.g...

if ((MyObject a = someMethod()) != null) {
a.doSomething();
}


MyObject someMethod() {
if (<condition>)
return new MyObject();
else
return null;
}

Cheers ;-)
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it. Both look like acceptable Java syntax. But it is probably not a good idea to return null from a method in "real life" at least not without dire warnings about "might return null."
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!