"Each one has to have a unique name" -- do you really mean that each one has to be bound to a unique identifier in the source text? Why not just put them in a Map, with the "name" as the key? Or make the name a field of SomeClass? I don't think you need the heavy artillery you're asking for.
–
Vance MaverickFeb 23 '11 at 0:37

what I mean is this: SomeClass s; SomeClass q; SomeClass p; s, q and p all have to be different. I have been told to just put them in a map, but that would require creating a large number of objects, many of which may never be needed. While that wouldn't always be an issue, im working with very processing-power / disk + RAM constrained machine.
–
reader1Feb 23 '11 at 0:46

2

What problem are you trying to solve? Perhaps there are better options.
–
OscarRyzFeb 23 '11 at 0:50

Without getting into too many details, I'm writing an application that runs within a larger program. Normally, the objects would get garbage-collected after I was done with them, but the larger program maintains them, thus the need for a unique name for each. If I don't give each a unique name, the old object will get overwritten, but it is still needed in the context of the greater program.
–
reader1Feb 23 '11 at 0:56

I think the real question is: why you think you need to do this?
–
Bryan KyleFeb 23 '11 at 0:57

5 Answers
5

What you're asking for is what some languages call MACROS. They're also sometimes known as preprocessor definitions, or simply "defines".

A decision was made to not have includes and macros and the like in Java because it introduces additional code maintenance concerns that the designers concluded was going to cause code that would not have been in the style they wanted.

However, just because it's not built into the compiler doesn't mean you couldn't add it to your build script.

As part of your build, you copy all files to a src-comp directory, and as you do, replace your tokens as they're defined.

No this is an answer. There are two questions. The first is can it be done at the Java code level. The answer is no because designers left it out. The second is if not how can it be done. The answer is with your own preprocessing script. I think it answers both questions quite well. Sure it has my personal commentary in it, but that's an integral part of this site is drawing upon others' experience.
–
corsiKaFeb 23 '11 at 0:47

I will look into this, thanks. I've tried getting into ant a few times, but lacked the motivation to do all the reading. Can ant help with this? If so this might be motivation enough.
–
reader1Feb 23 '11 at 0:50

I could see ant doing it yes. Ant tasks could cover the file move and the (perl? bash? java?) routine that does the conversion. Ant is a useful resource if you're a Java developer.
–
corsiKaFeb 23 '11 at 0:54

an own preprocessing script still would not allow creating new variables at runtime like in the loop of the question.
–
Paŭlo EbermannFeb 23 '11 at 1:03

What you describe (creating new named variables at runtime) is possible in interpreted languages like JavaScript, Lua, Bash, but not with a compiled language like Java. When the loop is executed, there is no source code there to manipulate, and all named variables have to be defined before.

Apart from this, your variables don't need a "unique" name, if you are using them sequentially (one after another), you could just as well write your loop as this:

If you really need your objects at the same time, put them in some sort of data structure. The simplest would be an array, you could use a Collection (like an ArrayList) or a Map (like CajunLuke wrote), if you want to find them again by key.

In fact, an array (in Java) is nothing else than a collection of variables (all of the same type), which you can index by an int.

(And the scripting languages which allow creating new variables on runtime implement this also with some kind of map String → (anything), where this map is either method/script-local or belonging to some surrounding object.)

You wrote in a comment to the question (better add those things to the question itself, it has an "edit" button):

Without getting into too many details, I'm writing an application that runs within a larger program. Normally, the objects would get garbage-collected after I was done with them, but the larger program maintains them, thus the need for a unique name for each. If I don't give each a unique name, the old object will get overwritten, but it is still needed in the context of the greater program.

So, you want to retain the objects to avoid garbage collection? Use an array (or List or anything else).

The thing is, if you want your larger program to be able to use these objects, you somehow have to give them to this larger program anyway. And then this program would have to retain references to these objects, thereby avoiding garbage collection. So it looks you want to solve a problem which does not exist by means which do not exist :-)

Your statement of "not with a compiled language like Java" implies you can't do this in other compiled languages like C or C++, where indeed you can do this there with Macros. Sure you don't define them as strings but they are simple text replacement which is what he's looking for.
–
corsiKaFeb 23 '11 at 1:00

@glowcoder: It looks like he wants to create new variables at runtime, not at compile-time (since he in fact is mixing the two).
–
Paŭlo EbermannFeb 23 '11 at 1:04

@ Paulo, I also said without getting into too many details. The program automatically grabs these objects as they are created. I will definitely be using either a HashMap or ArrayList or so in the short term. I will be looking into the build script solution as a long term fix. This is partially because I really need to learn to use Ant anyhow. I may or may not end up sticking with the HashMap/List solution. Perhaps I should switch my accepted answer? It was difficult to choose which one to accept as I am going to try to use both.
–
reader1Feb 23 '11 at 1:30

Huh, you are doing some strange magic here ... Anyways, creating new local variables in java is not the right way (as it is not possible at runtime in the VM, even with macros or a different JVM-based language. You could at most hide the usage of some data structure with some other syntactic construct.)
–
Paŭlo EbermannFeb 23 '11 at 1:38

-1 It would not be really cool. It was purposefully left out of the language because the designers didn't want it.
–
corsiKaFeb 23 '11 at 0:40

@glowcoder "because the designers" I think they were also the same ones that forced you to use classes all the time and didn't make functions first-class?
–
alternativeFeb 23 '11 at 0:43

@mathepic That would be them. Try maintaining a 3-million+ line app with includes everywhere and no coalescing of functionality. It's a nightmare (even if it does keep me employed :D ).
–
corsiKaFeb 23 '11 at 0:46

@glowcoder I'm not arguing whether or not the lack of macros is a good or a bad thing, just that "the designers made it so" is not good enough of a reason for it to be bad.
–
alternativeFeb 23 '11 at 0:57

@mathepic This will be a point of disagreement, but I believe it is a reason for it to be considered bad, at ~least~ within the realm of Java (which this question is in.)
–
corsiKaFeb 23 '11 at 1:01