Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating dynamic variable names

 
Vijaishanker bala
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible to create variable names at runtime, for example, I want to create an int variable with name currentSize+<aQueueName> and initialize it to 0.
so the final variable must look like int currentSizeQueueName1=0;

thanks

vijai
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no this is not possible. Why do you want to do this ?

Could and array or a java.util.Map be what you are looking for ?

Pascal
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pascal betz:
no this is not possible.


It is possible to generate Java code, compile it, load it, and run it, on the fly. It might therefore be possible to do what the original poster wants.

I agree with you, though, that the original poster is very likely confused about the real problem and that a solution using less esoteric techniques, is very likely to be possible and far preferable.
[ November 24, 2006: Message edited by: Peter Chase ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually when people ask questions like this, it's because they don't really understand how arrays and collections can be used. If you explain to us what you need to accomplish, we can tell you the right way to do it.
 
Vijaishanker bala
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for having replied so late. The problem so far, I have several printer queues(a data structure that extends ArrayList and has other parameters like a name,the number of jobs processed by that queue and the average processing time for that queue).
These printer queues are maintained in another ArrayList from which printer jobs maintained by each queue is executed by a Thread pool in a round robim fashion.
Using JMX, I have provided functionalities that enable the end user to add printer queues during execution time. In the JMX console, MC4J(name of the console), I have the possibility to plot graphs based on attributes, in this case,the number of jobs processed by that queue and the average processing time for that queue. The attributes accepted by the JMX console are primitive datatypes and arrays of those.
The Problem's kernel:I was thinking about having int variables, like
int CurrentJobs<queueName> = <some value>;
int AvgProcessingTime<queueName> = <some value>;
which keep getting updated and eventually show up in the JMX console as attributes. This is good for a fixed number of queues, but my problem is when the user adds queues dynamically, which means 2 int variables for that <queueName> need to be created, which can be eventually refreshed and shown up on the console.
Hope this description of my problem helps.

Thanks

Vijai
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need a separate number for those?
The queue itself should have a method for returning the number of jobs it contains for example, and the queues are all stored in a Collection.
 
Vijaishanker bala
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Jeroen, but the queue is a seperate object in itself, which extends ArrayList and also has instance variables, one of which holds the total number of jobs that were processed by the queue since startup of that application (or) since creation of that queue, although I get the number of jobs currently in the queue with the <queueName>.size() method.

Vijai
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, a small rant. IMHO, I don't think it is a good idea to do it this way. It is very easy to create and destroy mbeans on the fly, so why not create them as the queues are being created. From the console, you'll see the queue as another mbean, but isn't that better? As you'll see the queues and their known attributes, instead of one queue, with a long list of weird combination attributes? End-of-rant.


Back to the question... JMX is capable of sending notifications for attribute changes for attributes that are created on the fly. Notice that the attribute name (of the notification object) is a string, that can be set at runtime.

The problem here is that you are trying to store them with variables, that you want to create dynamically -- there is absolutely no reason to do that. Instead, you can store the values in a CurrentJob and ProcessingTime hashmap, and let the keys be the queue name.

Henry
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is good for a fixed number of queues, but my problem is when the user adds queues dynamically, which means 2 int variables for that <queueName> need to be created, which can be eventually refreshed and shown up on the console.


As the earlier messages were trying to tell you, storing objects with dynamically created names is a job for collections implementing the java.util.Map Interface. There are several implementations to choose from depending on how you want the collection to be organized. If queMap is one of these collections:

String key = "some unique name";
int[] qvar = new int[2] ; // your two int variables
queMap.put( key, qvar );

Now you can iterate through all the keys in queMap to make the display or retrieve the int[] to update the variables.

Bill
 
Vijaishanker bala
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never thought about having MBeans for each printer queue created dynamically, Henry wong, thanks for the suggestion, I tried that and it worked like a charm, each of the attributes being instance variables of the printerQueue object.
Thanks all for your help, I have managed to solve this problem without the use of having to create variables dynamically.

Vijai
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic