Any one have idea how to generate id starting from 1 so that the next object has 2 and so on?
I have trying the following but dose not work:
class students{
private int id;
private String name;
public student(String name){
this.id=id++;
this.name
}
}
You need a static class member to keep track of the last-used index. Be sure to also implement a copy constructor:
Update: As @JordanWhite suggest, you might like to make the static counter atomic, which means that it will be safe to use concurrently (i.e. in multiple threads at once). To that end, change the type to:
The increment-and-read operation and the reset operation become: