# Using ClassLoader with nodes and non-default constructors

**URL:** <https://discourse.openrobotics.org/t/using-classloader-with-nodes-and-non-default-constructors/2018>\
**Category:** ROS General\
**Tags:** ros2\
**Created:** [June 23, 2017, 1:41am UTC](https://discourse.openrobotics.org/t/using-classloader-with-nodes-and-non-default-constructors/2018 "2017-06-23T01:41:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Snowman](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Snowman](https://discourse.openrobotics.org/u/Snowman)\
**Post date:** [June 23, 2017, 1:41am UTC](https://discourse.openrobotics.org/t/using-classloader-with-nodes-and-non-default-constructors/2018/1 "2017-06-23T01:41:24Z")

</div>

I’m playing with the composition API in ROS2 (beta1) and ClassLoader::createInstance function takes only a string as a parameter (the name of the class being created). I need to pass a couple of other parameters to the constructor of my class - is there a way to do that?

---

<div class="post-metadata">

**Author:** ![TheDash](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/thedash/32/1858_2.png) [@TheDash](https://discourse.openrobotics.org/u/TheDash)\
**Post date:** [June 23, 2017, 2:24am UTC](https://discourse.openrobotics.org/t/using-classloader-with-nodes-and-non-default-constructors/2018/2 "2017-06-23T02:24:28Z")

</div>

The best way to do so currently is have your object read parameters from the rosparam server, and before you instantiate your object, set those params.

---

<div class="post-metadata">

**Author:** ![Paul\_Bovbel](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/paul_bovbel/32/21358_2.png) [@Paul\_Bovbel](https://discourse.openrobotics.org/u/Paul_Bovbel)\
**Post date:** [June 23, 2017, 2:39am UTC](https://discourse.openrobotics.org/t/using-classloader-with-nodes-and-non-default-constructors/2018/3 "2017-06-23T02:39:28Z")

</div>

I think the typical pattern is to create a default constructor for your plugin, but use a separate initialize(…) method to feed arguments.

See plugin definition packages such as nav\_core in the navigation stack, and controller\_interface in ros\_control.

---

<div class="post-metadata">

**Author:** ![tfoote](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/tfoote/32/3813_2.png) [@tfoote](https://discourse.openrobotics.org/u/tfoote)\
**Post date:** [June 23, 2017, 4:15am UTC](https://discourse.openrobotics.org/t/using-classloader-with-nodes-and-non-default-constructors/2018/4 "2017-06-23T04:15:13Z")

</div>

In general as mentioned by @TheDash and @Paul_Bovbel you must defer your initialization until later in the cycle.

To support loading components not known about at compile time we must require a standard uniform API of all components for the loader to interact with. Almost all plugin systems work with a minimum common API (often implemented in C++ as a base class) and a registration/discovery mechanism for the loader to find available plugins. Sometimes the registration/discovery also includes descriptions etc.

Using parameters is the recommended way to configure nodes as this is the way that configuration can be made at run time by users.

With respect to ROS 2. I’d suggest making sure you’re familiar with our [overview of composition](https://github.com/ros2/ros2/wiki/Composition) as well as our vision of a [managed node](http://design.ros2.org/articles/node_lifecycle.html) which adds a standard lifecycle on top which we can provide standardized tooling for managing ROS systems. This also gives a standard way to do initialization, configuration, and other transitions.

For comparison you can also explore the [nodelet API](http://wiki.ros.org/nodelet) in ROS1.

---

<div class="post-metadata">

**Author:** ![Snowman](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Snowman](https://discourse.openrobotics.org/u/Snowman)\
**Post date:** [June 23, 2017, 3:38pm UTC](https://discourse.openrobotics.org/t/using-classloader-with-nodes-and-non-default-constructors/2018/5 "2017-06-23T15:38:53Z")

</div>

Thanks everyone. Still wondering how could I possibly configure things such as node name or whether I should use intra-process communication. These values are required at the construction time, so initialization function is out of the question. Not sure if I can use parameters for those …

---

<div class="post-metadata">

**Author:** ![TheDash](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/thedash/32/1858_2.png) [@TheDash](https://discourse.openrobotics.org/u/TheDash)\
**Post date:** [June 23, 2017, 3:59pm UTC](https://discourse.openrobotics.org/t/using-classloader-with-nodes-and-non-default-constructors/2018/6 "2017-06-23T15:59:35Z")

</div>

Node name can be configured via rosparam strictly. e.g

ros.init\_node(ros.get\_param(“my\_node/prefix”) + “mynode”);

I can’t remember the exact API calls off the top of my head, but that’s the general approach. Above syntax wont work directly FYI, is what I’m trying to say.
