This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes creating dynamic variable names Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "creating dynamic variable names" Watch "creating dynamic variable names" New topic
Author

creating dynamic variable names

Vijaishanker bala
Ranch Hand

Joined: Sep 08, 2005
Posts: 82
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


When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." <br />Linus Torvalds
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
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

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Vijaishanker bala
Ranch Hand

Joined: Sep 08, 2005
Posts: 82
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

Joined: Apr 21, 2006
Posts: 1847
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.


42
Vijaishanker bala
Ranch Hand

Joined: Sep 08, 2005
Posts: 82
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
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
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

Joined: Sep 08, 2005
Posts: 82
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
 
GeeCON Prague 2014
 
subject: creating dynamic variable names