Jump to content

Adding id to variable name?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
AfroJack

AfroJack

    Newbie

  • Members
  • PipPip
  • 24 posts
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.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Just use a java.util.Map for this.

#3
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
The names of variables are created when you type them into the source file in an editor.

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Why don't you use an array?
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
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
What about creating something like:

class MyObject {

    private static int ID;

    private String name;

    public MyObject() {

        this.name = "Object"+ ID++;

    }

    ....

}



#6
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
Arrays.

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