In my Android project, I define several callbacks to work with button clicks, connection events, or user interface events such as Dilaog.onShow (). For demonstration purposes, I chose the Runnable interface, which should be launched from some activity code. With Java, I have different ways to express myself.
One of the patterns will be using an anonymous class.
runOnUiThread(new Runnable() { public void run() { doSomething(); } }); private void doSomething() { }
the other is for defining an inner private class, i.e.
private DoSomething implements Runnable { public void run() {
another is to use a private member, for example:
private final Runnable doSomething = new Runnable() { public void run() {
Here is another one that I like the most, because on the one hand it doesnโt actually create objects unless someone really uses it, because it avoids the extra classes, because it can take parameters if necessary.
private Runnable doSomething() { return new Runnable() { public void run() {
I'm not looking for arguments of taste or religious beliefs, but in terms of service and code performance. I would like to receive tips and advice that could help me develop my own preferences, perhaps different preferences in accordance with this circumstance.
Spoiler:
Java progress has made this question obsolete; see accepted answer.