Is this the right way to use private in Java?

Viewed 71
class ShopSimulation extends Simulation {
 18     private Event[] initEvents;
 29     private int numOfCounters;
 30     private int id = 0;
 31     private double arrivalTime;
 32     private double serviceTime;
 33
 34     public ShopSimulation(Scanner sc) {
 35         initEvents = new Event[sc.nextInt()];
 36         numOfCounters = sc.nextInt();
 37
 38         Shop initShop = new Shop(numOfCounters, initEvents.length);
 39         Customer[] customerList = initShop.getCustomerList();
 40         Counter[] counterList = initShop.getCounterList();
 41
 42         id = 0;
 43         while (sc.hasNextDouble()) {
 44             arrivalTime = sc.nextDouble();
 45             serviceTime = sc.nextDouble();
 46             Customer currentCustomer = customerList[id];
 47             // Set the customer arrivalTime and seriveTime
 48             currentCustomer.setTimings(arrivalTime, serviceTime);
 49
 50             initEvents[id] = new ArrivalEvent(arrivalTime, counterList, customerList[id]);
 51
 52             id += 1;
 53         }
 54     }

I am unsure if this is the right way to implement private in Java. What I understood about private is just to prevent it from being accessed by other class. Thus, if the variable is not used by other class, I should private it.

1 Answers

Yes its correct.

But its not a good practice to declare all the variable initialization statement at the beginning. You are not coding in c anymore.
Can leave some as default. No problem.

Just use the private musk tor the sensitive data.

Hope you find this useful .

Related