Instantiation and EGL interfaces of type JavaObject

When you use an interface of type JavaObject, you often need to access a static method that creates an object at run time. This requirement is in effect because the EGL interface technology does not allow you to instantiate a Java™ object from within your EGL code.

Consider the following EGL source file:
  package myInterfaces;

  Interface HelloWorld type JavaObject
    {javaName = "HelloWorld", 
     packageName = "com.ibm.examples.helloWorld"}
     
    function sayHello(name String) 
						 returns (String);
  End
The related Java class would need to be available at both compile time and run time:
  package com.ibm.examples.helloWorld;

  class HelloWorld
  {
     // Java class constructor
     public HelloWorld()
     {
     }

     // implementation of the function 
     // that is referenced in the interface
     public String sayHello(String name)
     {
       return "Hello " + name;
     }
   }
An additional EGL Interface part is appropriate, however:
  package myInterfaces;

  Interface HelloWorldFactory
    {javaName = "HelloWorldFactory", 
     packageName = "com.ibm.examples.helloWorld"}
    static function createHelloWorld() 
      returns (HelloWorld);
  End
The Interface part HelloWorldFactory refers to the following Java class, which includes a static method that returns an object of type HelloWorld:
  package com.ibm.examples.helloWorld;

  class HelloWorldFactory
  {
    // Java class constructor
    public HelloWorldFactory()
    {
    }

    // implementation of the function 
    // that is referenced in the interface
    public static HelloWorld createHelloWorld()
    {
      return new HelloWorld;
    }
  }
An EGL program might include the following code:
  package myDriver;
  import myInterfaces.*;

  program myProgram { }
  
    function myFunction()
      echo STRING;

      // create a variable of type HelloWorld;
      // this reference variable points to NIL
      hwVar HellowWorld;

      // assign a Java object to the variable
      hwVar = HelloWorldFactory.createHelloWorld();

      // access any Java method that is in 
      // the object of type HelloWorld 
      // and has a corresponding
      // function description
      // in the EGL interface part
      echo = hwVar.sayHello("John Doe");
    End
  End

When you are writing a pageHandler that interacts with the JSF component tree, you create variables that are based on EGL interface parts; however, you are not concerned with object creation because EGL handles the issue at run time. For an overview, see JSF component tree.

Related concepts
EGL interfaces
JSF component tree

Related tasks
Creating an EGL Interface part

Related reference
Interface part in EGL source format
Interfaces of type JavaObject

Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.