7

Java helps us creating anonymous object using

new class_name();

statement and calling the methods using association(.) operator like

new Emp().input();

How can I use it to invoke two methods simultaneously from an anonymous object like invoking both input() and show() together ?

7
  • 3
    you cannot. because the object is not referenced, it is basically lost once the method returns. Some apis use the fluent interface pattern, but it is still quite rare. Commented Jul 23, 2014 at 17:06
  • 3
    you need to call Emp e = new Emp(); e.input(); e.show();. Why is that not an acceptable solution, btw? Commented Jul 23, 2014 at 17:06
  • @njzk2 Not really anonymous if you assign it to a name ;) Commented Jul 23, 2014 at 17:09
  • 1
    @aruisdante: exactly. My comment should be rephrased : why do you need the object to be anonymous? Commented Jul 23, 2014 at 17:10
  • 1
    Just as a general piece of advice, I would caution against trying to make your code too terse. The fluent pattern is fine, but on the other hand, so is writing this as three lines. There have been lines that did a zillion things at once that I've looked at and thought, "it'd be more readable if we split these up" -- but it's very rare that I look at 3 or 4 lines and think, "this would be more readable if we smushed these into one line." Commented Jul 23, 2014 at 17:18

3 Answers 3

13

or

public Emp show() {
    // do the stuff
    return this;
}
public Emp input() {
    // do the stuff
    return this;
}

Then call with

new Emp().show().input();
Sign up to request clarification or add additional context in comments.

4 Comments

That would be the fluent interface pattern mentioned by @njzk2
don't forget to replace void with the return type :-)
The voids should be Emp. :)
Thanks paul and Roddy for the fix
4

How about making a method:

public void inputThenShow() {
    input();
    show();
}

Then call with

new Emp().inputThenShow();

4 Comments

Method ShowAndInput() should be showAndInput() camel cased with lower first letter to follow Java coding style + conventions. Also, personally, I'd want the method to be called inputThenShow() or similar to indicate the order of the implemented actions.
This should really be an exception and not the norm. Generally it is very bad to have the word and in your method names.
Why is it bad? And there is no and in the method name.
@Zach The original version did. But I think what skiwi is referencing is the idea of a method having a single responsibility, which is a common, a generally considered "good", practice in OO. Its not so much the word "and" itself, its the principle violation which it implies (at least I think that's what skiwi meant).
1

What you can also do, without modifying the Emp class, is create an anonymous class that extends your class to allow it to call both methods.

new Emp() {
    public void doStuff() {
        input();
        show();
    }
}.doStuff();

Which as a bonus gives you an anonymous instance of an anonymous class.

1 Comment

Except when it (here: Emp) is a final class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.