File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes about istantiating a class at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about istantiating a class at runtime" Watch "about istantiating a class at runtime" New topic

about istantiating a class at runtime

Marco Vanoli
Ranch Hand

Joined: Jan 12, 2005
Posts: 99
i' m in trouble istantiating a class at runtime and calling its method.

I have a javabean and i must use Beans.setDesignTime(true) for using it.
Then i have fc that is a String name of the class i want to istantiate at runtime. This string is variable relative to user input; so i cannot know the exact class name and i must use the superclass FuzzySet to declare it & then use use newIstance() for istantiating my custom class.

The problem is that now i have a FuzzySet istance and i cannot use specific metoth of my custom class.. there are many case and every possible istance of the beans has its different properties that i have to set (from use inpu)

Is there some way to create a variable at runtime that not is the superclass?


[ September 25, 2005: Message edited by: Marco Vanoli ]

bye, <br />Marco
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You seem to have a bit of a contract with the user here. They believe they can enter the name of FuzzySet or any subclass and you can do something with it. To keep that contract, you'll need to keep the code you have. If the user enters an invalid class you'll get a class cast exception, which is only fair because the user messed up on the contract.

The class they enter may or may not be one of the subclasses you'd like to work with. You can check to see if they gave you one you like.

Look up "instanceof" and "casting" in a good language book (or just Google) to see the full story on how they work.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Marco Vanoli
Ranch Hand

Joined: Jan 12, 2005
Posts: 99
Tnk but would like to use something as reflection.
I explain better my situation:

The user choose a javabeanclass ( we trust is right ) from a list ,
then the program interrogate with reflection the class and give to the user the properties of the javabeanclass without istantiating it.
(attention: every class has differents properties)

Then the user give the value for all properties of the the class he choose.
Here comes the problem:
now i have to set really the javabeans properties so i think i need to istantiate it.
It is possible to make a program that can automatically istantiate a given classname(a java bean) and then set its properties(the properties are all the time differents in refer to the class name used)?
I need to do this possibly not hardcoded all classname but leave java to do all the work using only the String with the class name and value of its properties.

Maybe this is not possible in a normal java program but is possible using some specific javabean api functions.

tnk you
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 692
Yes, it can be done in java. See the Reflection classes for how to get info about a class.
To get at the 'properties' of a class you would need to get a list of all the methods it has. Then look for those whose names begin with 'get' or 'set' to find the 'properties'. The definition of a property is that it has a get<Property>() and set<Property>() method.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can do all this with reflection.

Use Class.forName() to get the Class but no object instance. Look up Class in the doc and find a way to get all the methods. For any method names that start with "set" assume the rest of the name is a property name, eg "setSize" implies a property "size".

Show that list of property names to the user and let them enter values. If you want to be really cool, inspect the types of the parameters and restrict data entry to valid data such as integers or dates.

When you get the user data make a bean instance, go through those setX methods again and invoke them. Look up Method in the doc to see how to call them.

Good clean fun!
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> Class fuzzyClass = Class.forName("full.Fuzzy." + fc);
>> FuzzySet theFuzzyset = (FuzzySet)fuzzyClass.newInstance();

Here's a safer solution. The class "name" must reference a Type or a sub-class of Type. This eliminates the cast but it only works in Java 5.0 which has better type safety facilities all around.

Type type = Class.forName(name).asSubclass(Type.class).newInstance();

SITE NOTE: When you're writing software you have specific classes that give you a context. Take advantage of that context and use more general method and variable names: "jump" versus "catJump" and "list" versus "fleaList".

If you use generic method and variable names where possible, your code starts to take on a familiar symmetry across the board. This makes it easier to maintain and debug.
[ September 26, 2005: Message edited by: Rick O'Shay ]
I agree. Here's the link:
subject: about istantiating a class at runtime
It's not a secret anymore!