CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 1999
    Location
    India.
    Posts
    9

    Instantiation of Objects at run Time.

    Hello,
    I am having a problem that I cant instantiate an object by the normal method of new [Classname](),
    instead I have a variable which contains the name of the class whose object is to be instanttiated.
    This is for sure that the Class whose object is to be fired will be present in the library.
    To make my thing clear: e.g: consider the code

    String var = new String("");
    var = GetClassName();
    // Now I cant say something like Object Obj = new var; where var has the name of the class whose object I can instatiate. I want to just fire that Obect's method after that.
    Can U please make it as fast as possible because I know the solution is there but I am unable to implement anything on this.
    Thanks a million
    sumit



  2. #2
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    Re: Instantiation of Objects at run Time.

    I think u can load any class at Runtime using 'ClassLoader' class. U can retrieve ClassLoader instance from 'obj.getClass().getClassLoader()'.

    Then u can try getting instance of the class u want to instantiate using 'findClass' or 'loadClass' methods.

    - UnicMan

  3. #3
    Guest

    Re: Instantiation of Objects at run Time.

    Here is it

    Use java.lang.Class - first use the string var that contains the class name with forName() ie Class myclass = Class.forName(className);(className is a String that contains the class name - case matching must).Then u should have a no arg constructor in ur class.Call myclass.instance() which will create a instance of ur class.Then us the object invoke ur methods.In case u dont have a no arg consturctor then u should use method of java.lang.Class that returns all ctors of ur class and use a suitable ctor to create the object
    Hope this is clear


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured