
Untitled
By: a guest on
Jul 14th, 2012 | syntax:
None | size: 0.95 KB | hits: 12 | expires: Never
How do I automacially assign a variable in constructor while creating objects
final class Foo {
private static final AtomicInteger seed = new AtomicInteger();
private final int id;
public Foo() {
this.id = seed.incrementAndGet();
}
}
public class YourClass {
private static int generalIdCount = 0;
private int id;
public YourClass() {
this.id = generalIdCount;
generalIdCount++;
}
}
class Person {
private static int nextId = 1;
private final int id;
Person() {
id = nextId++;
}
}
public class Person {
// same across all instances of this class
static int currentCounter = 0;
// only for this instance
int personId;
public Person(){
personId = currentCounter;
currentCounter++;
}
}
public class Person{
public static int Increment = 1;
public int ID;
public Person(){
this.ID = Increment;
Increment++;
}
}