How to create a class dynamically in java

Technically, you can, via a bytecode manipulation library - CGLIB, javassist, asm, bcel and the likes.

However, this is not the Java "philosophy". Java is statically-typed, so you'd better create the classes before runtime.

Take a look at hibernate / eclipseLink for Java ORM - a way of mapping tables to objects.


I think what you want is the facility provided by java.lang.reflect.Proxy and related classes.


What would you do with a dynamically created and instantiated class that none of your other code knows about?

For a stically typed language like Java, it makes little sense to have such classes. On the other hand, most OR Mappers like Hibernate come with tools that allow you to statically generate classes from a database schema.