In the past few weeks, too many sentences have ended with "... this will make sense when we learn about classes." Well today, we learn about classes.

Say we are making a student directory app. The app displays a list of students and their associated details, such as name, age, and phone number. How will we store this information?

Let's start with names. We might store a student's name like this:

String studentName1 = "Bob";

Adding other details:

String studentName1 = "Bob";
int studentAge1 = 18;
String studentPhone1 = "12345678";

What about other students? We might do something like this:

String studentName1 = "Bob";
int studentAge1 = 18;
String studentPhone1 = "12345678";

String studentName2 = "Susan";
int studentAge2 = 19;
String studentPhone2 = "12345678";

This is starting to get clunky. Think about how you will use this information. What if I want to print each student's name?

Well, we've learn about arrays, so you might think of simplifying like so:

String[] studentNames = {"Bob", "Susan"};
int[] studentAges = {18, 19};
String[] studentPhones = {"12345678", "12345678"};

This makes it easier to iterate through. But there are still some difficulties here. Can you think of any?

We are using three separate lists to keep track of the details. Logically, that seems a bit strange doesn't it? If we were storing this on paper, we wouldn't use one list of names and a separate list of ages. Things might go out of sync. For example, if we delete a name but not an age, all the details will be off sync, because we were using the array index to match the details together. If we were doing this on paper, we might have one page for each student, and have a stack of pages. We need some way to move from separate lists to having a list of students.

Wouldn't it help if we had a way to represent a single student, where we could store the details of that student? We could then just work with a list of students. This is what classes and objects allow us to do.

Watch this video that explains this concept: https://www.youtube.com/watch?v=NUl8lcbeN2Y. Ignore any mentions of VBA or Access; those are the specific languages/tools used in that course. The concepts are identical.

Read this article about objects: https://docs.oracle.com/javase/tutorial/java/concepts/object.html

We will be learning how to create and use objects in the following lessons. For a taste of what we are going to be writing, read this article: https://docs.oracle.com/javase/tutorial/java/concepts/class.html. The new syntax will make sense over the next few lessons.

results matching ""

    No results matching ""