I am doing some coding in Java, but it doesn't work:
public class job
{
private static int count = 0;
private int jobID;
private String name;
private boolean isFilled;
public Job(, String title, ){
name = title;
isFilled = false;
jobID = ++count;
}
}
I need to auto-increment the Id when a new entry is created.
Try this:
public class Job {
private static final AtomicInteger count = new AtomicInteger(0);
private final int jobID;
private final String name;
private boolean isFilled;
public Job(String title){
name = title;
isFilled = false;
jobID = count.incrementAndGet();
}
Use the following,
public class TestIncrement {
private static int count = 0;
private int jobID;
private String name;
private boolean isFilled;
public TestIncrement(String title) {
name = title;
isFilled = false;
setJobID(++count);
}
public int getJobID() {
return jobID;
}
public void setJobID(int jobID) {
this.jobID = jobID;
}
}
Please use following to test this
public class Testing {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < 10; i++) {
TestIncrement tst = new TestIncrement("a");
System.out.println(tst.getJobID());
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With