Java class with no constructor?

前端 未结 5 1276
心在旅途
心在旅途 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:51

    Yes, you need a constructor to assign parameters to your class during object creation, or you could reference each variable individually though the instance and assign it, e.g:

     Customer c = new Customer();
     c.id = 0;
     // etc
    

    A constructor just makes this easier as you do not need to write more code than necessary.

    I do recommend using constructors to assign various values to a class instance instead of doing it manually every time you create an instance of an object.

提交回复
热议问题