Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a method in a class in each other method

Tags:

java

I have a method that I want to be called each time one of the other methods is called. I dont want to have to explicitely call it each time. Is there a way to have the other methods naturally call that method before executing its own code?

If I have a method thats called isThere() and I want it called in each other method, I dont want to have isThere() written in each method. I was hoping there would an easier way to do this.

like image 348
True_Blue Avatar asked Apr 19 '11 17:04

True_Blue


People also ask

Can you call a method in another method?

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.

Can a method call another method in the same class?

Similarly another method which is Method2() is being defined with 'public' access specifier and 'void' as return type and inside that Method2() the Method1() is called. Hence, this program shows that a method can be called within another method as both of them belong to the same class.


1 Answers

You should look into AOP - Aspect Oriented Programming.

Since you are using Java, I recommend you to take a look at AspectJ.

like image 188
Jonas Kongslund Avatar answered Sep 29 '22 05:09

Jonas Kongslund