Reputation: 619
I have 3 classes in my Java application, One representing a Medical Policy:
public class MedicalPolicy implements PolicyType {
private int id;
// and some other fields
public MedicalPolicy(final LocalDate effective,final LocalDate expiry,final ArrayList<Beneficiary> beneficiaries){
//Constructor that does NOT set/initialize the id field
}
}
Another class which represents the Motor Policy:
public class MotorPolicy implements PolicyType{
private int id;
// and some other fields
public MotorPolicy(final LocalDate effective,final LocalDate expiry,final double vehiclePrice){
//Constructor that does NOT set/initialize the id field
}
}
And the 3rd class contains the main()
method.
Every time I declare and initialize a Medical Policy object in my main()
I want the id
of the Medical Policy to increment by 1, the same for the Motor Policy, i.e if I declare 2 Medical Policy objects in my main()
I want the value of 1st Medical Policy to have the id
equal to 1 and the 2nd Medical Policy to have the id
's value set to 2, and if I declare 3 Motor Policy, I want the 1st to have it's id
equal to 1, the 2nd's id
equal to 2 and the 3rd's id
equal to 3.
How can I accomplish that?
P.S: There is no prob if I have to set/initialize it from the constructor, what's important is that I shouldn't pass it's value as a constructor parameter.
Upvotes: 1
Views: 3052
Reputation: 20185
As I have suggested in my comment, I would add an AtomicInteger
to count the number of constructor calls:
public class MedicalPolicy implements PolicyType {
private static final AtomicInteger counter = new AtomicInteger();
private final int id;
public MedicalPolicy(
final LocalDate effective,
final LocalDate expiry,
final ArrayList<Beneficiary> beneficiaries){
id = counter.incrementAndGet();
}
}
The reason for using an AtomicInteger
is thread-safety.
Notice, however, that this implementation does only count how often the constructor was called. To get a count of reachable instances of a given class there are two solutions coming to my mind. One involves overriding finalize()
(which is deprecated in Java 13). The other uses a List
of PhantomReference
s to track reachable instances.
Upvotes: 2
Reputation: 2228
You can use AtomicInteger, which is used in applications such as atomically incremented counters.
public class MedicalPolicy implements PolicyType {
private static final AtomicInteger count = new AtomicInteger();
// and some other fields
public MedicalPolicy(final LocalDate effective,final LocalDate expiry,final ArrayList<Beneficiary> beneficiaries){
//Constructor that does NOT set/initialize the id field
count.incrementAndGet();
}
}
Upvotes: 1
Reputation: 3166
Use a private static int
as an instance variable
Inside the constructor you set the id to the serial and add one to the serial.
public class MedicalPolicy implements PolicyType {
private static int serial = 1;
private int id;
// and some other fields
public MedicalPolicy(final LocalDate effective,
final LocalDate expiry,
final ArrayList<Beneficiary> beneficiaries){
this.id = serial++;
// Other constructor stuff
}
}
If your class needs to be thread safe, use an AtomicInteger
as Turing85 mentioned.
Upvotes: 1
Reputation: 15
Could you not create a List, then each object of that class would be associated to its incrementing location in the index. eg. listName.get(obj num-1) since the first object will be stored at index of 0. Also listName.size() would return a value so you could track how many you had.
Upvotes: -1