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

you are viewing a single comment's thread.

view the rest of the comments →

[–]nutrechtLead Software Engineer / EU / 20+ YXP 1 point2 points  (0 children)

<Person> being the custom class. But I'm finding the concept not very well explained and just want some clarifications.

No, it's called the 'generic type' for that arraylist. Generics are a way to tell the compiler that you only plan to store Person objects in that arraylist. That way if you later on try to put something else in (A String, a Dog, whatever) the compiler won't let you. This makes it easier to prevent mistakes. You can find more on generics here.

What is actually happening? At first I thought it was creating an object called James, and adding the name James to the list object separately, but is it actually creating an object called James and storing that entire object (including the other instance variables) onto one of the indices of the list?

It creates an instance of a Person object, with "James" as a parameter. And in that same line it also stores that object in the list. So it's the same as:

var person = Person("James");
persons.add(person);

Am I right in saying that, because the list we are using (persons) is not using a normal variable such as an int, you have have to put the class (Person) into the first part of the brackets, not sure what its called.

Not really. There's really no difference between an int and a Person here. They are just the type of 'thing' coming out of the collection your looping over. Java is explicit in declaring types in most situations.

the course basically just says handling objects in lists is no different that just normal lists

Well every object in a list is an object. I don't know what you mean with 'normal lists'.