Connecting Tech Pros Worldwide Help | Site Map

creating dynamic VARIABLE in java

Newbie
 
Join Date: May 2007
Posts: 1
#1: May 9 '07
Please help me to creating dynamic VARIABLE in java, with predefined name.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: May 9 '07

re: creating dynamic VARIABLE in java


Quote:

Originally Posted by Bora Ji

Please help me to creating dynamic VARIABLE in java, with predefined name.

You can't without substantial byte code fiddling. Better use a Map<String, Object>
in which you store the associations between names and values.

kind regards,

Jos
Newbie
 
Join Date: May 2007
Posts: 3
#3: May 12 '07

re: creating dynamic VARIABLE in java


what do you want to do?
Newbie
 
Join Date: May 2009
Posts: 1
#4: May 15 '09

re: creating dynamic VARIABLE in java


HOW TO DO THIS ?


//Variable Declaration
String col_width_1 = "100px";
String col_width_2 = "150px";
//etc String col_width_N = "XXXpx";

//loop
for (int h = 0; h < hrecset.getRowCount(); h++)
{
//some code
//create the variable name with a concat with the loop counter variable name to get the value of the concatened variable
out.println("<td style=\"width: "+col_width_+h+" \">")//Do you understand what i mean?
//some code
}
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: May 15 '09

re: creating dynamic VARIABLE in java


Use an array or read my first reply; btw, don't hijack threads; it is considered rude. Start your own thread instead.

kind regards,

Jos
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#6: May 16 '09

re: creating dynamic VARIABLE in java


Quote:

Originally Posted by slauper View Post

Expand|Select|Wrap|Line Numbers
  1. for (int h = 0; h < hrecset.getRowCount(); h++)
  2. {
  3.  //some code
  4. }
  5.  

Don't use this .. because every time the system executes hrecset.getRowCount().
Instead use,

Expand|Select|Wrap|Line Numbers
  1. int rowCount = hrecset.getRowCount();
  2. for (int h = 0; h < hrecset.getRowCount(); h++)
  3. {
  4.  //some code
  5. }
  6.  
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#7: May 18 '09

re: creating dynamic VARIABLE in java


Quote:

Originally Posted by dmjpro View Post

..
Instead use,

Expand|Select|Wrap|Line Numbers
  1. int rowCount = hrecset.getRowCount();
  2. for (int h = 0; h < hrecset.getRowCount(); h++)
  3. {
  4.  //some code
  5. }
  6.  

Possibly a typo on your part but you posted the same code that he used.
Reply