クラスの初期化に関して、ベストプラクティスを知りたいのですが、
Customer c = new Customer();
つまり、クラスをトップレベルで一度初期化し、クラス内のすべての場所で使用する必要があります。
Tools tools = new Tools();
public boolean doCIdCheck(int cId) {
final Iterator<Customer> cursor = tools.Customers.iterator();
while (cursor.hasNext()) {
if (cursor.next().getCIdCheck(cId)) {
return true;
}
}
return false;
}
または、new Customer().checkCId();
必要な場所で使用する必要があります。
public boolean doCIdCheck(int cId) {
final Iterator<Customer> cursor = new Tools().Customers.iterator();
while (cursor.hasNext()) {
if (cursor.next().getCIdCheck(cId)) {
return true;
}
}
return false;
}
または、各関数/メソッドに独自のクラスのインスタンスを持たせるのが最善です。
public boolean doCIdCheck(int cId) {
Tools tools = new Tools();
final Iterator<Customer> cursor = tools.Customers.iterator();
while (cursor.hasNext()) {
if (cursor.next().getCIdCheck(cId)) {
return true;
}
}
return false;
}