dinamic classloader?

Discussion in 'Java' started by Novello, Jul 1, 2005.

  1. Novello

    Novello Guest

    Hello,

    My program needs to create a class, see code below to understand ;-)

    /**
    * Loads system's class and execute it
    */
    private static void loadSystem(String systemName,String symbol) {

    // wrong code:
    systemName system = new systemName(symbol);

    }

    the first param is systemName, the name of the class to create.
    I cannot create it such as "systemName system = new sytemName(symbol)"
    because systemName is a String and not a Class.

    How can I solve this problem?

    If something is not clear please ask, I will explain better :)

    thank you
     
    Novello, Jul 1, 2005
    #1
    1. Advertising

  2. Andrew Thompson, Jul 1, 2005
    #2
    1. Advertising

  3. Novello

    John Currier Guest

    There's currently a discussion titled "Defining a constructor in an
    Interface" in this newsgroup...the initial post should give you a
    starting point.

    John
    http://schemaspy.sourceforge.net
     
    John Currier, Jul 1, 2005
    #3
  4. Novello

    Chris Smith Guest

    Novello <> wrote:
    > /**
    > * Loads system's class and execute it
    > */
    > private static void loadSystem(String systemName,String symbol) {
    >
    > // wrong code:
    > systemName system = new systemName(symbol);
    >
    > }
    >
    > the first param is systemName, the name of the class to create.
    > I cannot create it such as "systemName system = new sytemName(symbol)"
    > because systemName is a String and not a Class.


    Google for "reflection". The basic pattern looks like this:

    private static void loadSystem(String systemName,String symbol)
    throws ...
    {
    Class c = Class.forName(systemName);
    Constructor ctor = c.getConstructor(String.class);
    Object system = ctor.newInstance(symbol);
    }

    (* This is 1.5 code. In 1.4 and previous versions, you'll need to
    explicitly instantiate the Class[] in the getConstructor call, and the
    Object[] in the newInstance call. 1.5 makes this cleaner. See the API
    docs for the appropriate signatures for all of those methods.)

    (** I've left out exception handling. When using reflection, there are
    a gazillion things that might go wrong, so you should handle the
    exceptions.)

    Note that the reference you get back is of type Object, not of the
    specific class you loaded. That's actually the best you can do; the
    compiler obviously doesn't know what methods or fields are available for
    that dynamic class. To use the object, you will need to do one of two
    things: cast the object to an interface or superclass type that declares
    the operations you need, or use reflection again to find the methods and
    fields and to access them.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jul 1, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alessandro

    Problems with Dinamic controls

    Alessandro, Jul 16, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    419
    Cristian Suazo
    Jul 16, 2003
  2. Damir

    Dinamic images

    Damir, May 6, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    387
    Damir
    May 6, 2005
  3. whois
    Replies:
    12
    Views:
    593
    Roedy Green
    Jun 22, 2004
  4. Roberto Dias
    Replies:
    1
    Views:
    754
    Paolo Alexis Falcone
    Jun 30, 2004
  5. Morgan
    Replies:
    7
    Views:
    456
    CBFalconer
    Feb 7, 2004
Loading...

Share This Page