File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes getting instance of a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "getting instance of a class" Watch "getting instance of a class" New topic

getting instance of a class

anita sam

Joined: May 24, 2005
Posts: 11
How to get the instance of a class using the class "Class". can anyone help me with this. thanks....
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 31634

Welcome to JavaRanch! The forName() method does what you are looking for:

Note that
MyClass c = MyClass.class.getInstance() and
MyClass c = new MyClass()

are safer as they give compile time safety checks.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
anita sam

Joined: May 24, 2005
Posts: 11
hi Jeanne,
thanks for your response.

my code is

class cl{
String cname1;
public void test(String cname)
System.out.println("instance of the class is :" + Class.forName(cname1));

catch(ClassNotFoundException ce)
System.out.println("exception thrown" +ce);
public class useclass {

public static void main(String[] args) {
cl ccl = new cl();

I wrote this to get the instance of class using the Class's forName().
can any of you tell me is there is any error in this if not, what will be the output.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11688

can any of you tell me is there is any error in this if not, what will be the output.
is there a reason why you don't just try compiling/running this yourself to find out the answer?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189


Your program won't create an instance of the class: it will only load the class. Of course, that doesn't mean much, since your class "cl" is already loaded, since it's running -- but I assume you're just using that as an example.

In any case, forName() is only half of the puzzle. It loads the .class file and gives you a Class object. Now you need to ask the Class object for the instance you want:

Class clazz = Class.forName("cl");
cl myCl = (cl) clazz.newInstance();

This works only if the class in question has a default (no-argument) constructor. If it doesn't, then this will raise an exception. If that's the case, you can still do it, but it's more complex: you need to ask the Class object for a list of Constructor objects, and then pick one to invoke, passing the proper arguments. But this isn't done very often: most of the time classes intended to be loaded this way have a default constructor (applets, servlets, etc. all must have one.)

[Jess in Action][AskingGoodQuestions]
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1024
Originally posted by Ernest Friedman-Hill:

Class clazz = Class.forName("cl");
cl myCl = (cl) clazz.newInstance();

The only modification I might make is to declare myCl to be an Object instead of explicitly a cl. This is because forName() is usually run with a String variable that isn't known at edit/compile time. (If you knew at compile time that the arg to forName() was going to be "cl", then you should have just used new cl().) Even if you know at compile time that you're going to use "cl", declaring myCl as a Object delays the loading of the cl class until the forName() is executed, which is similar behavior to the standard forName()/newInstance() idiom.

Class clazz = Class.forName("cl");
Object myCl = (cl) clazz.newInstance();

Instead of Object, you might be able to specify some other class that is expected to be the superclass for any class retrieved with forName(). For instance, an application like Tomcat might use forName() to load the servlet object specified by an incoming URL. In that case, it would assume that new object will be a subclass of Servlet. (...which is basically what Mr. Friedman-Hill said in his last paragraph.)

anita sam

Joined: May 24, 2005
Posts: 11 am clear
I agree. Here's the link:
subject: getting instance of a class