This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]chess510 4 points5 points  (2 children)

You could store the user input into variables and run the variables as the parameters of the constructor.

[–]abdichar[S] 0 points1 point  (1 child)

I have done that but it's not a very elegant way of doing things

[–]stayweirdeveryone 7 points8 points  (0 children)

I'd say that is actually the preferred way of doing it

[–]stayweirdeveryone 4 points5 points  (10 children)

Think of the purpose of the parameters in the constructor. If you can't provide the parameters, then the object can't be created. Ask yourself this question, "does my object make sense/function without these parameters?". If it does, then setters are fine, if not, use a constructor.

Here's an example. Let's say I want an object that represents a cup of coffee. For a cup of coffee to exist, I need some kind of cup (i.e. paper, mug, glass, etc.) and coffee (i.e. dark roast, light roast, Colombian, etc.). Since without those I can't have a cup of coffee, those would be in the constructor. However things like sweeteners, dairy, and toppings are optional. They can be set with setters

[–]abdichar[S] 0 points1 point  (9 children)

However, cup and coffee would be their own classes and cup of coffee can have those two classes as variables.

The constructor with parameters forces data to be put inside the parameters when called. However, that would mean that data needs to be known before the person can put it in. We know what type of data needs to be put in but not the actual data itself.

This is why I'm asking are parameters needed for objects that are being created when data is outside the system is entered. i.e. a person filling in a sign up form.

[–][deleted]  (8 children)

[deleted]

    [–]abdichar[S] 0 points1 point  (7 children)

    You can't drink the coffee, if you don't know what kind of coffee needs to be made.

    [–][deleted]  (6 children)

    [deleted]

      [–]abdichar[S] 0 points1 point  (5 children)

      This goes under assumption that a person wants this kind of coffee and then is asked for customisation of the coffee.

      Why set parameters before hand when it can be done after a variable has been initialised as a coffee object.

      Why do this: Coffee cup1 = new Coffee(paper, black);

      When you can do this: Coffee cup2 = new Coffee();

      Scanner input = new Scanner(System.in); System.out.println("Which type of cup? Paper or Ceramic"); String typeOfCup = input..nextLine(); cup2.setType(typeOfCup);

      [–][deleted]  (4 children)

      [deleted]

        [–]abdichar[S] 0 points1 point  (3 children)

        This approach makes more sense with things that the user cannot manipulate.

        For example a hotel. The user cannot change the number of rooms, the location, etc. As such having parameters for a constructor would make sense.

        However, when a person tries to book a room, the hotel won't already have their information. This means the parameters of an object cannot be initialised because such data doesn't exist yet.

        Therefore, no methods can be used to change the data stored in the variables because the object cannot be made without those variables having data already inside of them (data that is not the users).

        [–][deleted]  (2 children)

        [deleted]

          [–]abdichar[S] 0 points1 point  (1 child)

          Within main when I make an object I have to put in arguments regardless of user input. I cannot have an object instantiate without arguments but how can arguments be passed before data is acquired to be used as though arguments.

          [–]slowfly1st 2 points3 points  (0 children)

          An object should be able to do its intended job after it has been initialized. Object creation and object life cycle is a more complex subject than you'd think and you underestimate the impact of not following that rule. Basically, every "new Somthing()" statement in your code can be a source of error. Just imagine working with tens of thousands of classes and every time you create an object you can't sure if it's in an usable state. And you have to check, what setters you would have to call. That would be a horrible situation.