2

I wrote a constructors that can passes all the fields, including an Arraylist. I don't know what to do if I want constructors where I'm not passing the Arraylist and instead give it an empty Arraylist.

For example I already wrote a Course class and now I'm writing a Student class. The student class contains an Arraylist.

class Student {
    String studentFirstName;
    String studentLastName;
    ArrayList<Course> studentSchedule = new ArrayList<Course>();

    // Constructors
    Student(String newFirstName,  String newLastName) {
        this.Student(newFirstName, newLastName, _______ ); //what to put in the blank?
    }

    Student(String newFirstName, String newLastName, ArrayList<Course> newSchedule) {
        this.studentFirstName = newFirstName;
        this.studentLastName = newLastName;
        this.studentSchedule = newSchedule;
    }
    .
    .
    .

I'm stuck here. Putting null in the blank does not work, I get compiler warning: The method Student(String, String, null) is undefined for the type Student Obviously I'm missing the point.

How do I get the constructor to give me an empty Arraylist?

4 Answers 4

5

Well, you want to pass an empty ArrayList, so pass an empty ArrayList:

class Student {
    String studentFirstName;
    String studentLastName;
    List<Course> studentSchedule; // no initialization here: it's done in the constructor

    // Constructors
    Student(String newFirstName,  String newLastName) {
        this(newFirstName, newLastName, new ArrayList<Course>());
    }

    Student(String newFirstName, String newLastName, List<Course> newSchedule) {
        this.studentFirstName = newFirstName;
        this.studentLastName = newLastName;
        this.studentSchedule = newSchedule;
    }

Note that you should generally declare the field as List<Course>, not as ArrayList<Course>, unless it's really important for the list to be an ArrayList, and not any other kind of list. Program on interfaces.

Also, your fields should be private, and shouldn't be named studentXxx. They're part of the Student class, so it's redundant. lastName is sufficient, and more redable.

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

1 Comment

+1 for mentioning the List interface and best naming practices. One could additionally consider a defensive copy like this.studentSchedule = new ArrayList<Course>(newSchedule); to prevent the list from being modified after it has been passed to the constructor.
0

To pass an empty ArrayList:

this(newFirstName, newLastName, new ArrayList<Course>());

(Also note that the correct syntax is this., not this.Student.)

Comments

0

Just construct an empty ArrayList and pass it to the constructor:

Student(String newFirstName,  String newLastName) {
    this(newFirstName, newLastName, new ArrayList<>());
}

Also, notice that I've used this() to call Student's other constructor, as opposed to this.Student() which is invalid.

Notice, though, that you are already initializing the ArrayList field at the point of declaration. You should consider removing this initialization if you want it to take place via the constructor.

Comments

0

Probably passing new empty instance of Arraylist is your question if I got it correct

this (newFirstName, newLastName, new ArrayList<Course>())

Or, you can pass null also,

this (newFirstName, newLastName, null)

Edit :

After @Erwin's comment, to invoke constructor of same class, you should use this() only.

1 Comment

'this.Student(' does not compile.

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.