Java class with no constructor?

前端 未结 5 1442
心在旅途
心在旅途 2021-01-27 16:46

I have this class

class Customer{
    int ID;
    Time arriveTime;
    Time serviceTime;
    Time completeTime;
    int transaction;
}

Don\'t I

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-27 17:38

    The fields can be set from outside the class, meaning you can use another class to set the values.

    A constructor however is a more convenient way to do this indeed and can shield manipulations to the fields by another class (implemented by some programmer who might disturb the intended functionality).

    In case a class only is implemented to store a record, you can make the fields private and final and implement the appropriate getters.

提交回复
热议问题