creating java objects at runtime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaya3
    New Member
    • Aug 2007
    • 184

    creating java objects at runtime

    Hi all,
    only during runtime , i know the class for which i need to create the object.How to generate objects for different classes at run time??
    Please do needfull..


    -Hamsa
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by gaya3
    Hi all,
    only during runtime , i know the class for which i need to create the object.How to generate objects for different classes at run time??
    Please do needfull..


    -Hamsa
    Have a look "Class" class in Java.

    Debasis Jana

    Comment

    • gaya3
      New Member
      • Aug 2007
      • 184

      #3
      Originally posted by dmjpro
      Have a look "Class" class in Java.

      Debasis Jana

      Hi dmjpro....
      Thank u....I tried with following
      Class CN = Class.forName(" ClassName");
      Object o = CN.newInstance( );
      For that i m getting InstantiationEx ception...How to resolve??

      -Hamsa

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by gaya3
        Hi dmjpro....
        Thank u....I tried with following
        Class CN = Class.forName(" ClassName");
        Object o = CN.newInstance( );
        For that i m getting InstantiationEx ception...How to resolve??

        -Hamsa
        May be you have no default "Constructo r" or you have that "Constructo r" as "private" or "protected"
        Have a through look at the "API".
        Actually "Why this Exception thrown" !
        Also having look at "Constructo r" and "Method" classes.
        This will help you to resolve that problem with that particular "class".

        Debasis Jana

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by gaya3
          Hi all,
          only during runtime , i know the class for which i need to create the object.How to generate objects for different classes at run time??
          Please do needfull..


          -Hamsa
          [CODE=java]ClassName object = new ClassName();[/CODE] creates an object from a class called ClassName.

          Comment

          • gaya3
            New Member
            • Aug 2007
            • 184

            #6
            Originally posted by dmjpro
            Have a look "Class" class in Java.

            Debasis Jana

            Hi dmjpro,
            Thank u...
            could u please clear me following doubt...

            Class c1 = Class.forName(" x1.x2.x3.servic e.DropdownServi ce");
            when i print "c1" its printing as
            " interface x1.x2.x3.servic e.DropdownServi ce"

            but i need "class" for that DropdownService ... what to do??
            please do needfull...


            -Hamsa

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Originally posted by gaya3
              Hi dmjpro,
              Thank u...
              could u please clear me following doubt...

              Class c1 = Class.forName(" x1.x2.x3.servic e.DropdownServi ce");
              when i print "c1" its printing as
              " interface x1.x2.x3.servic e.DropdownServi ce"

              but i need "class" for that DropdownService ... what to do??
              please do needfull...


              -Hamsa
              You want the implemented Classes?

              Debasis Jana

              Comment

              Working...