MaDKit forum

Welcome All !
Please, use explicit topic names when creating new threads. Thanks.

You are not logged in.

#1 2016-03-08 19:25:10

oxality
Member
Registered: 2016-02-24
Posts: 10

Launch nested/inner class

I was wondering if there was a way to launch an inner class from the outer class. For example

class outerClass 
{
    main()
    {
          ///code
          new Madkit("--launchAgents", innerClass.class.getName() +",true;" + ConsoleAgent.class.getName());
     }

class innerClass extends scheduler
{ 
     activate()
    {
         //code
    }
}
}

This gives me an InstantiationError, i guess because it cant find the correct class name?

Last edited by oxality (2016-03-08 19:26:11)

Offline

#2 2016-03-09 10:52:23

fmichel
Administrator
From: Montpellier
Registered: 2009-03-24
Posts: 172
Website

Re: Launch nested/inner class

oxality wrote:

This gives me an InstantiationError, i guess because it cant find the correct class name?

Not exactly. In fact, to instantiate an inner class you need to do it from an existing instance of the enclosing class, i.e. outerClass here, which cannot be the case when you only pass the name of the inner class to the kernel.

So, for doing this you can use the following :

public class InnerAgentClassTest {
	    public static void main(String[] args) {
	   	 InnerAgentClassTest test = new InnerAgentClassTest();
	         Madkit m = new Madkit("--launchAgents", ConsoleAgent.class.getName());
	         m.doAction(KernelAction.LAUNCH_AGENT, test.new InnerClass(),Boolean.TRUE);
	     }

	class InnerClass extends Scheduler
	{ 
		@Override
		protected void activate() {
			super.activate();
		}
	}
}

Offline

#3 2016-03-10 01:36:23

oxality
Member
Registered: 2016-02-24
Posts: 10

Re: Launch nested/inner class

ah ok. is there a way to pass values to the new Agent when you launch it, similar to the way you can pass values when constructing an object? Ive noticed AbstractAgents ignore/dont require constructors

Offline

#4 2016-03-10 11:00:35

fmichel
Administrator
From: Montpellier
Registered: 2009-03-24
Posts: 172
Website

Re: Launch nested/inner class

oxality wrote:

ah ok. is there a way to pass values to the new Agent when you launch it, similar to the way you can pass values when constructing an object? Ive noticed AbstractAgents ignore/dont require constructors

AbstractAgents  subclasses can have their own constructor and thus be parametrized when instantiated before being launched using the launchAgent(madkit.kernel.AbstractAgent) version. Is it what you are looking for or do you have another use case in mind ?

Offline

#5 2016-03-11 00:22:30

oxality
Member
Registered: 2016-02-24
Posts: 10

Re: Launch nested/inner class

This worked, thanks. However now the panel for that AbstractAgent doesnt pop up. Any output set through logger just goes to my IDE terminal. Im just using the inherited setupFrame method if that matters

Offline

#6 2016-03-11 10:45:30

fmichel
Administrator
From: Montpellier
Registered: 2009-03-24
Posts: 172
Website

Re: Launch nested/inner class

oxality wrote:

This worked, thanks. However now the panel for that AbstractAgent doesnt pop up. Any output set through logger just goes to my IDE terminal. Im just using the inherited setupFrame method if that matters

Well, it should just be because you have to use the version of the method that includes the boolean parameter so that the agent has a GUI or not, i.e. launchAgent(a,true). Does this solve your problem ?

Offline

#7 2016-03-15 18:45:50

oxality
Member
Registered: 2016-02-24
Posts: 10

Re: Launch nested/inner class

That did it. Thank you

Offline

Board footer