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 creating simple java objects dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "creating simple java objects dynamically" Watch "creating simple java objects dynamically" New topic

creating simple java objects dynamically

Jochen Szostek
Ranch Hand

Joined: Mar 23, 2008
Posts: 42

Hi all,

Is it possible to generate an object dynamically within java code (at runtime)?

Something like:

somekindofjavaobject user;

user.addVar(Public String userName);
user.addVar(Public String password);

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46410
Not like that, no.

After the declaration of the object you have a null reference, so the app would crash with a NullPointerException.

That is, if the compiler would let Public instead of public or a method call with three tokens past its Argus-like eye in the first place.

You might manage to get a method using the Class.forName method.

Myclass myObject = Class.forName("mypackage.MyClass").getInstance();


I think that might require MyClass have a no-arguments constructor.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Well actually... yes, it is quite possible. Check out, for example, ASM. This library lets you build a class file from code; you can then load that class and use it right in the application.

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

Joined: Feb 18, 2005
Posts: 1044
You might consider using a Hashtable (from the java.util package) or similar object.

Jochen Szostek
Ranch Hand

Joined: Mar 23, 2008
Posts: 42

Thanks a lot for the fast replies!!

Gonna try the reflections and ASM approach first thing tomorrow.

The hashtable solution isn't really going to be useful I guess because want to use the (in runtime) generated object in hibernate so I can store it in my DB.
(hope this will be possible, though I guess I also need to adjust my hibernate mapping settings in real time somehow too)

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46410
If you get the reflection to work, tell us, then I can see where I was mistaken.

Jochen Szostek
Ranch Hand

Joined: Mar 23, 2008
Posts: 42

Well, if I get it right then "Class.forName()" can be used for dynamic class loading. But then (for what I'm trying to do) I'd have to create and compile my dynamic class somehow during runtime...and I don't really see how I can accomplish that...

I could use ASM to generate the bytecode for the class somehow but I browsed a bit through the docs on their site and it doesn't seem that easy to me. I think I will need quite some time to get it.

To tell you the whole story: The kind of system I would like to create is one where you can generate a form (with some fields) which then would automatically be mapped to a java object with the same name as the form name and the fields and their values as class variables. Thereafter I would like to pass it to hibernate to store it in my DB. (so then I would need to generate hibernate's mapping files too along the way)

I've already been working on such a of system in PHP and managed to get it more or less, but now I'm trying to create the same thing in Java.

Maybe I could use the struts ModelDriven interface for generating objects from html forms as someone advised me in this thread:

But I'm not sure if this workflow would be suited for the kind of system I'm trying to build. I'm still exploring all my any comments are welcome.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I use Javassist for the same purpose, and can recommend it. It's easy to get started with, and has very helpful tutorial to get up and running.

As an aside, it's possible to drive the Java compiler from inside Java code, and quite easy. But using one of the above-mentioned libraries is probably easier.
I agree. Here's the link:
subject: creating simple java objects dynamically
It's not a secret anymore!