Search code examples
anylogic

AnyLogic: Execute Java Function which is embededded in the Main agent from a Java Class


I want to execute a java function in a java class. The function is embedded in the Main agent and I want to call this function from a java class. How can I do that? Do I have to instantiate a main agent? Or do I have to declare my java class as a subclass of Main?


Solution

  • one way is to add main as a variable of your class, so you can add it in the constructor of your class so then you can just do main.yourFunction(); anywhere in your class.

    public class YourClass{
       public Main main;
       public YourClass(Main main){
           this.main=main;
       }
    }
    

    so when you instantiate your class you do new YourClass(main);