Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting instance of a class

 
anita sam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How to get the instance of a class using the class "Class". can anyone help me with this. thanks....
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anita,
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.
 
anita sam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jeanne,
thanks for your response.

my code is

class cl{
String cname1;
public void test(String cname)
{
cname1=cname;
try
{
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();
ccl.test("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.

thanks....
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anita:

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.)
 
Ryan McGuire
Ranch Hand
Posts: 1055
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)

Ryan
 
anita sam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks....now am clear
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic