Nav

Example: To Create an Instance of a Class

This example shows how to create an instance of a class:


       
    
1
<java:new class="com.foo.Person" constructor="Person()"/>

This example shows how to create an instance using the default constructor. You can also use a non-custom constructor.


       
    
1
2
3
4
5
6
<java:new class="com.foo.Person" constructor="Person(String, Integer)">
    <java:args>#[{
      firstName: 'MG',
      age: 34
    }]</java:args>
</java:new>
When configuring the constructor arguments in the args parameter, the keys of the map will identify which parameter should be invoked. In order to reference the parameters by name (firstName, age, etc.), the java class containing the Method or Constructor has to be compiled using the -parameters compiler flag. If the class was not compiled with this flag, then the same parameters should be refenced in the canonical way as arg0, arg1, etc.

Note that you will get DataSense on the operation’s output (in this case a Person object), and you will use DataSense to help you build the arguments map.

The classes used with the Java Module must be exported, if not the execution will fail with a JAVA:CLASS_NOT_FOUND error. See How to export resources.