Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One method called in several methods

Tags:

java

I have a class that looks like this:

class A {
    public void method1(){
        iniCall();

        // Do something
        finalCall();
    }

    public void method2(){
        iniCall();

        // Do something different
        finalCall();
    } // ... more methods like this
}

How can I simplify this iniCall and finalCall so not to write them in every function (or several functions)?

Is it possible to do something like call(method1), being call something like this:

public void call(method){
    iniCall();
    method();
    finalCall();
}

Otherwise, what is a good alternative?

like image 880
zurfyx Avatar asked Dec 15 '15 15:12

zurfyx


People also ask

Can you call a method within a method?

Java does not support “directly” nested methods. Many functional programming languages support method within method. But you can achieve nested method functionality in Java 7 or older version by define local classes, class within method so this does compile.

Can we call one method from another method in Java?

We can call a method from another class by just creating an object of that class inside another class. After creating an object, call methods using the object reference variable.

What is calling method and called method?

A method is a routine that applies to a particular class of objects. Once an object is declared, you can refer to it by its identifier when calling methods. You can call methods for a window that has not previously been declared using a special identifier for dynamic instantiation.

Can I create many methods with the same name?

Yes, we can define multiple methods in a class with the same name but with different types of parameters.


3 Answers

EDIT I see that my answer raises some questions.

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread.

So creation of an instance of this interface may lead to uncertainty and questions. As suggested in comments, you may want to write your own interface and use it instead. It may be something like this:

public interface Method {
    public void run();
}

Your call method will change to something like this:

public void call(Method method) {
    iniCall();
    method.run();
    finalCall();
}

And your call to this method will be something like this:

call(new Method() {
    public void run() {
        // do your stuff here
    }
});

I suggest that you use similar design or consider using of a Template pattern which will probably require some refactoring.

like image 145
Yury Fedorov Avatar answered Sep 23 '22 12:09

Yury Fedorov


You can use a lambda expression (or anonymous class instance that implements Runnable if you don't use Java 8) :

public void call(Runnable method){
    iniCall();
    method.run();
    finalCall();
}

...

public void someMethod () {

    call (() -> {
                   // do something
                });
    ...
    call (() -> {
                   // do something different
                });

}
like image 30
Eran Avatar answered Sep 27 '22 12:09

Eran


Similar to the other suggestions (who are much quicker than me - but I always want to provide a running example...), I'd suggest passing a Runnable instance to a method that generically calls iniCall(), executes the Runnable and then calls finalCall().

The actual work may be passed in as dedicated Runnable instances, lambdas or method references - the latter is shown here:

public class RepeatedMethodContents
{
    public static void main(String[] args)
    {
        RepeatedMethodContents r = new RepeatedMethodContents();
        r.callWrapped(r::doSomething);
        r.callWrapped(r::doSomethingDifferent);
    }

    private void callWrapped(Runnable runnable)
    {
        iniCall();
        runnable.run();
        finalCall();
    }

    private void doSomething()
    {
        System.out.println("doSomething");
    }

    private void doSomethingDifferent()
    {
        System.out.println("doSomethingDifferent");
    }

    private void iniCall()
    {
        System.out.println("ini");
    }

    private void finalCall()
    {
        System.out.println("final");
    }

}

The output is, as desired:

ini
doSomething
final
ini
doSomethingDifferent
final

BTW: I'd be hesitant to use reflection in general for things like this. Most often, there is an easier, more straightforward and less error-prone way.

like image 5
Marco13 Avatar answered Sep 27 '22 12:09

Marco13