Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Declaring Member Variables Dynamically

  Asked By: Dukker    Date: Nov 10    Category: Java    Views: 1504
  

Does anybody know how to dynamically declare member variables in a class?

for instance: when u have 8 records in you DB then you will need to create 8 variables
but when more records inserted you will need more member variables correspondingly.

Share: 

 

8 Answers Found

 
Answer #1    Answered By: Ann Evans     Answered On: Nov 10

easiest way is to use a hashmap instead of a class.
you can dynamically  genereate classes in Java
but if you like this kind of features it is better to use JRuby or Groovy.
since java is a statically typed language.

 
Answer #2    Answered By: Dan Romero     Answered On: Nov 10

Thank u very much, Arash.
but in this case, I need a class rather than a structure like HashMap. also as such rare occasions like this does not happen in everyday programming, I rather stick to Java anyway.

 
Answer #3    Answered By: Clint Garcia     Answered On: Nov 10

If I understand correctly, you need to create  a class in runtime and load it dynamically  and instantiate it.

I remember a class library called "Code Generation Library" or "cglib" is usually used for such scenarios.



I know it is used for creating dynamic classes in interception and aspect oriented frameworks.

 
Answer #4    Answered By: Wilfred Young     Answered On: Nov 10

what I needed seemed closer to what Hamed suggested.
I need the class to be generated and instantiated dynamically,
but some parts of this class should be static.

OK, let me tell you the scenario which is very simple and you might
have encountered that many times before.
suppose there is a table with n records  (limited) and I'm implementing a submit form through a class definition which should produce TextFields, Checkboxes and ...for every record (dynamic section), there is also some EventHandling and blablabla for these fields (static section)

good to say that:
1- I'm not using any JSP's or scripts and I don't really think I need any POJOs (althogh helpful).
2- I'm using Hibernate but I really rather do this task independently (by only having the records count).
3- I also thouroughly revised java.lang.reflection but I didn't see any mean to declare  them in runtime, but only to dynamically  access the fields which was declared before.
4- I do not know Groovy and JRuby, and not planning to cover them by now, so changing the solution would be a safer faster way to reach the goal preferebly.

 
Answer #5    Answered By: William Bouchard     Answered On: Nov 10

In Hibernate, You can use dynamic-map or dom4j entity mode of Hibernate (also dynamic component can be usefull)
If you use your custom DAO framework you can do it by java reflection ( CGLIB is better about performance )
But how dynamic member  variables can be usefull for your real project ? it means use maps instead of dynamic member variables

 
Answer #6    Answered By: Jean Bell     Answered On: Nov 10

"castor" also allows you to define dynamic object model and it generates POJOs in compile time and map them to a data representation in runtime, this way you can alter tables and deliver a patch with a new xml object model. this can be used with DAOs too

ibatis (the new version) could also be another option

 
Answer #7    Answered By: Dominic Murphy     Answered On: Nov 10

Would you please let me know why you need 8 variables  when you have 8 records  in DB? As far as I know, you should map your records to your object instances while you are mapping DB fields into member  variables of a class. To hold a list of instances corresponding to your DB records, I think you should use Collections or Maps.

 
Answer #8    Answered By: Jeffrey Washington     Answered On: Nov 10

Any how, if you would be interested in any Free Development classes then please let me know. I am IBM partner and provide Free Classes on their behalf.

 
Didn't find what you were looking for? Find more on Declaring Member Variables Dynamically Or get search suggestion and latest updates.