Re: Distinct ID Number Per Object?
Hal Vaughan <hal@thresholddigital.com> writes:
It took me a bit to think through this. Do you mean making a
static int and each instance uses it as an ID, then increments
it for the next one? That's what I got, or rather, worked out.
class globalCounter { private static int value = 0;
public static int getValue(){ return value++; }}
class Identifier
{ final private java.lang.String prefix;
private int count;
public Identifier()
{ this.prefix = java.lang.String.valueOf( globalCounter.getValue() );
this.count = 0; }
public java.lang.String get()
{ return prefix + "-" + java.lang.String.valueOf( count++ ); }}
public class Main
{ final static java.lang.String lineSeparator =
java.lang.System.getProperty( "line.separator" );
public static void main( final java.lang.String[] args )
{ final Identifier identifier0 = new Identifier();
final Identifier identifier1 = new Identifier();
java.lang.System.out.println
( identifier0.get() + lineSeparator +
identifier0.get() + lineSeparator +
identifier0.get() + lineSeparator +
identifier1.get() + lineSeparator +
identifier1.get() + lineSeparator +
identifier1.get() + lineSeparator ); }}
0-0
0-1
0-2
1-0
1-1
1-2