0

I have this class

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

Don't I need a constructor in order to set the values inside? I will be using this class to hold different values while making an array of Customers. Wouldn't this mean I would need this in order to set the values?

public Customer(int id, Time arrive, Time service, Time complete, int trans){
            ID = id;
            arriveTime = arrive;
            serviceTime = service;
            completeTime = complete;
            transaction = trans;
}

I only need the Customer class to hold the information about each customer.

2
  • Unless you specify some constructor Java supplies an implicit no-argument constructor that just sets everything to zero. Commented Mar 5, 2014 at 23:52
  • Note that the above is a perfectly legitimate equivalent to a C/C++ "struct" -- a not-quite object that groups data items together without encapsulating them. Not every situation calls for encapsulation -- sometimes it's just extra work. Commented Mar 5, 2014 at 23:55

5 Answers 5

4

Not necessarily, as by default your attributes (values) without a visibility modifier can be set directly on instances of Customer from code in the same package.

For example:

Customer c = new Customer(); // default constructor 
c.ID = 5;
... (etc.)

More on modifier access levels here.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

You didn't define a constructor at all, so the default constructor will be added for you. It will, however, not set any of those fields, so you'll have to go:

Customer c = new Customer();
c.ID = 34

And so on. A couple of things:

  • You didn't declare an access modifier, so the classes in the same package will be able to call c.ID but others won't. You might want to declare them public
  • But, it would probably be better if you created a getter and setter method instead of giving access to your fields. This way you could change the fields in the future without changing the interface

Comments

0

The method that you are suggesting does not do a very good job of abstracting and separating variables. It allows them to be messed with by any code at will.

Part of Java is OOP, or Object Oriented Programming. If you make certain parts of the class private and separated from the rest of the code, then it will limit access from other code, which helps your class follow the OO principle of encapsulation.

Instead, I would make a constructor that fills in the fields when appropriate, such as arrive and id. I would make the variables private. Also, I would make id a static member, because it would be incremented for every new customer.

Comments

0

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.

8 Comments

How does a constructor make it easier?
So you do not have to assign the various parameters each time you create an instance of an object, the constructor does it instead. Saves how much time you spend writing your code.
So what's the real difference between writing new Fred(a, b, b) and new Fred(); instance.a = a; instance.b = b; instance.c = c;? Yes, you save typing about 3 dozen characters, but that's peanuts in most cases.
I'm not saying there is a difference in the outcome of the code, I am saying that it is easier to use a constructor instead of assigning the fields individually, especially when you need to create many instances of an object, with many parameters that need to be different.
I'm sorry, I don't see any remarkable difference in ease. In fact, if the values to be assigned must be calculated somehow it's advantageous to have the assignments on separate lines.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.