How do I pass parameter when using newInstance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    #1

    How do I pass parameter when using newInstance

    Hello!

    I use method forName and newInstance in class Class like this
    instance = (GameFactory)(C lass.forName(na me).newInstance () );

    Now to my question:
    Assume that name is GameFactory then I want to pass a parameter to the
    GameFactory constructor
    like this instance = (GameFactory)(C lass.forName(na me).newInstance (field) );
    Here I pass parameter field which is an int but when I do so I get compile
    error saying

    F:\gameLab\Game Factory.java(15 ): Cannot find method 'newInstance(in t)' in
    'java.lang.Clas s'

    How you any idea?

    Or is it so that if I want to pass parameter I have to explicitly use new
    GameFactory(fie ld);

    //Tony







  • Oliver Wong

    #2
    Re: How do I pass parameter when using newInstance


    "Tony Johansson" <johansson.ande rsson@telia.com > wrote in message
    news:Ldq5f.1486 27$dP1.506466@n ewsc.telia.net. ..[color=blue]
    > Hello!
    >
    > I use method forName and newInstance in class Class like this
    > instance = (GameFactory)(C lass.forName(na me).newInstance () );
    >
    > Now to my question:
    > Assume that name is GameFactory then I want to pass a parameter to the
    > GameFactory constructor
    > like this instance =
    > (GameFactory)(C lass.forName(na me).newInstance (field) );
    > Here I pass parameter field which is an int but when I do so I get compile
    > error saying
    >
    > F:\gameLab\Game Factory.java(15 ): Cannot find method 'newInstance(in t)' in
    > 'java.lang.Clas s'
    >
    > How you any idea?
    >
    > Or is it so that if I want to pass parameter I have to explicitly use new
    > GameFactory(fie ld);[/color]

    In short, you can't do this. From the JavaDocs:

    <quote>
    Creates a new instance of the class represented by this Class object. The
    class is instantiated as if by a new expression with an empty argument list.
    </quote>

    - Oliver


    Comment

    • Ian Shef

      #3
      Re: How do I pass parameter when using newInstance

      "Tony Johansson" <johansson.ande rsson@telia.com > wrote in
      news:Ldq5f.1486 27$dP1.506466@n ewsc.telia.net:
      [color=blue]
      > Hello!
      >
      > I use method forName and newInstance in class Class like this
      > instance = (GameFactory)(C lass.forName(na me).newInstance () );
      >
      > Now to my question:
      > Assume that name is GameFactory then I want to pass a parameter to the
      > GameFactory constructor
      > like this instance =
      > (GameFactory)(C lass.forName(na me).newInstance (field) ); Here I pass
      > parameter field which is an int but when I do so I get compile error
      > saying
      >
      > F:\gameLab\Game Factory.java(15 ): Cannot find method 'newInstance(in t)'
      > in 'java.lang.Clas s'
      >[/color]
      <snip>

      I don't think that you can do this with Class, but you can use the reflection
      package java.lang.refle ct . Use Class.getConstr uctor or
      Class.getConstr uctors to get an appropriate Constructor. Then use
      Constructor.new Instance to create and initialize an instance.

      It is going to be more complex than the no-argument constructor that you were
      using before.

      Good Luck!


      --
      Ian Shef 805/F6 * These are my personal opinions
      Raytheon Company * and not those of my employer.
      PO Box 11337 *
      Tucson, AZ 85734-1337 *

      Comment

      Working...