vijrox
vijrox

Reputation: 1156

Is it possible to call a method of an object while passing that object into a function?

Say I have a method Foo createFoo(). I need to call the method boolean Foo.bar() on the object returned by Foo createFoo(), then pass the object into a method void baz(Foo someFoo). Is there a way to do this in one line?

If I didn't need to call boolean Foo.bar(), I could have done

baz(createFoo());

but I can't do this, because it doesn't call Foo.bar().

I also can't do this:

baz(createFoo().bar());

because that would pass into baz the boolean returned from Foo.bar(), not the actual Foo object.

Upvotes: 1

Views: 43

Answers (1)

aioobe
aioobe

Reputation: 420951

No, if you need bar to return boolean you'll have to use

Foo foo = createFoo();
foo.bar();
baz(foo);

If you find yourself repeating this pattern over and over, you can of course also create a convenience method

void barAndBaz(Foo foo) {
    foo.bar();
    baz(foo);
}

and do

barAndBaz(createFoo());

Upvotes: 5

Related Questions