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?
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.
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.
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.
Yes, we can define multiple methods in a class with the same name but with different types of parameters.
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.
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
});
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With