Hey I'm making multiple objects, and I'd like them to be named Object1, Object2 etc. I want the number to increase everytime a new object is made, but I'm not sure how to add a static variable to an object name (this is what I thought I'd do). Any help will be greatly appreciated, thanks.
5 replies to this topic
#1
Posted 16 February 2012 - 03:38 AM
|
|
|
#2
Posted 16 February 2012 - 04:34 AM
Just use a java.util.Map for this.
#3
Posted 16 February 2012 - 05:13 AM
The names of variables are created when you type them into the source file in an editor.
#4
Posted 16 February 2012 - 07:11 AM
Why don't you use an array?
object[0]
object[1]
object[2]
object[3]
...
and so on.
object[0]
object[1]
object[2]
object[3]
...
and so on.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#5
Posted 16 February 2012 - 05:40 PM
What about creating something like:
class MyObject {
private static int ID;
private String name;
public MyObject() {
this.name = "Object"+ ID++;
}
....
}
#6
Posted 21 February 2012 - 11:46 AM
Arrays.
Then you can make object[0], object[1], and so on. A simple for loop to make a lot.
Then you can make object[0], object[1], and so on. A simple for loop to make a lot.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









