• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a Class on-the-fly

 
Dinesh Sundrani
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a text file called class_content_decider.config and I would like to develop a simple Java Application (TestDynamicClass.java) that would read this 1-liner config file and create a DYNAMIC class (object on the fly).

Say for example - class_content_decider.config
firstanme, lastname, age, dob, company, salary

And the dynamic Class/Object should look like

Class temp1_21
{
String firstanme;
String lastname;
String age;
String dob;
String company;
String salary;
}

Would Reflection help in such a (specific) case?

Please suggest!

Regards,
DS - [SCJP, SCWCD, SCDJWS]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reflection works on existing classes, but a library like Javassist can easily create classes on the fly.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However you would certainly need to use reflection to create and access instances of that class after you had created it.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless it implements an interface.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple Map<String,String> looks easier to accomplish.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic