Results 1 to 3 of 3
  1. #1
    nurkhasanah is offline Member
    Join Date
    May 2010
    Rep Power

    Default Class.forName(ObjectName).newInstance()

    I've problem with Class.forName(ObjectName).newInstance()

    I don't know the mean of that code. Could everybody explain it to me?
    What's the function of Class.forName() ???
    What's "newInstance()" mean ?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power


    You have more problems than that if you think this is a Swing/AWT question. Or is it that you can't be bothered to put in the effort to post in the correct subforum?


  3. #3
    Join Date
    May 2010
    Rep Power


    Class.forName(...).newInstance() is a way of creating an instance of a class at runtime using the Java Reflection API. Here is an example:

    String className = ""java.util.Vector";
    Class cls = Class.forName(className);
    List list = (List)cls.newInstance();

    The cls.newInstance() calls the default constructor to create a new instance of the class.

    Normally you would use reflection when you want to determine the implementation at runtime instead of at compile time. This is the main concept behind Dependency Injection. You define interfaces and provide the implementation of the interface at runtime through some configuration.

    I hope this answers your question.

Similar Threads

  1. class.forName(" ");
    By srikantha in forum Advanced Java
    Replies: 2
    Last Post: 08-13-2009, 02:23 PM
  2. Class.forName Exception
    By Moncleared in forum Advanced Java
    Replies: 5
    Last Post: 02-21-2009, 07:08 AM
  3. Replies: 1
    Last Post: 03-14-2008, 03:51 PM
  4. about Class.forName
    By angus203 in forum New To Java
    Replies: 0
    Last Post: 11-25-2007, 05:47 AM
  5. question about Class.forName()
    By oregon in forum JDBC
    Replies: 4
    Last Post: 08-01-2007, 05:52 PM

Posting Permissions

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