Values that belong to each other
Student: OK enough with defining variables. I already have some programming experience. This class is supposed to be about “an object-oriented approach”; I’m quoting right from the CSE catalog. What does that mean?
Prof: I’m glad you asked! Sometimes that gets put off until later. We should address it right away.
Student: Good, now we’re getting somewhere!
Prof: OK, I’m guessing you’ve used the Triton Student System a little bit?
Student: Yeah…
Prof: What are some kinds of data stored in the TSS?
Student: Um… well our TSN, courses, what we enrolled in, stuff like that.
Prof: Great. What do you think is stored about courses? Let’s focus on this course. What’s stored about CSE11?
Student: Uh, the name, the times, the waitlist, the room it’s in, the list of students…
Prof: OK. So if you had to write down, using just what we’ve seen, the course name, max size and current enrollment count what might you write?
Student: Hm… so I have to make up some numbers. With variables that’d be like…
Prof: Sure, seems plausible. Now do it for CSE12.
Student: OK, more made-up numbers
Prof: Sure, but what if you wanted both in the same program?
Student: Oh, you mean like all together? Just put them all in the same file.
Prof: Let’s try it.
Student: Oh, I can’t have the same definition twice. Well I could just add stuff to the names, like
maxSize2 or something.Prof: Sure, that would run. Would you be satisfied?
Student: Well, not really. It seems like a course should be… like a thing that we store all together.
Prof: Great insight. In Java there is a feature called
records that lets us describe data that “goes together”. That idea—multiple pieces of data that “go together” in a record—is going to be key to thinking about objects.Student: Oh, I see, so each one prints as like
Course, and then the square brackets with the stuff inside.Prof: Exactly, and that “stuff” is called the fields of the record. We say that
Course is a record with three fields: name, maxSize, and currentSize. Each field has a name and a type.Student: So each field is like a definition!
Prof: Almost: there’s one part of a definition they are missing, what is it?
Student: The value! But they all have values, that’s what printed out.
Prof: You’ve hit on an important distinction. The
record defines the shape of a Course – it has three fields, one is a String and the others are ints. When we construct a course with new Course, we give the values for its fields.Student: Hmm… that was a complicated sentence.
Prof: It’s a little bit like when we do
new Course(...), we fill in the values for 3 definitions at once. We can even see them by using the . operator:Prof: So we have 6 values, like in the long example with all the variables. They are just stored inside the two different
Course instances that we made by using new. The record line on its own didn’t do anything with specific values like 600 or "CSE11". It just set up the shape so we could use new and bundle the values together.Student: Ok. I see. So every time we make some things with new we access the fields with
.?Prof: Not always. It’s just that you can; I just wrote those all out for the example.
Student: So what do we do with these?
Prof: I love how eager you are! We will see soon. For now I’ll ask that you trust me that
record and new will be critical going forward. Oh and also I called the things stored in cse11 and cse12 instances. Another word for them is objects. So that directly answers one of your questions: a lot of our course will be concerned with programming with objects, which we create with new and can shape with things like record.Student: Alright, I’ll stick with you a little longer!
Practice
A program says:
record User(String username, int followerCount) {}
User fan = new User("tritonfan", 1200);What prints for the fan definition?
fan = User[username=tritonfan, followerCount=1200]fan = User[tritonfan, 1200]Each value is labeled with its field’s name.fan = new User("tritonfan", 1200)new User(...)is how the value gets built, not how it prints.
Here the values for new User are in a different order than the fields. Decide what will happen before you run it.
What happens?
- It doesn’t compile: the values for
new Usermust match the fields in order — aString, then anint. - It works: Java matches
"tritonfan"tousernamebecause it’s the onlyString.Java matches values to fields by position, not by type. The first value goes to the first field, whatever it is. - It works, and prints
User[username=1200, followerCount=tritonfan].Each field has a name and a type, and the type is checked:usernameis aString, and1200is anint.
Users write posts. Complete the program so that
Post is a record with String fields content and author and an int field likes, and first is a post by "tritonfan" with no likes yet. Post( content, String author, likes) {}
first = Post("first day of class!", "tritonfan", 0);
first = Post("first day of class!", "tritonfan", 0);
A program contains only this one line:
record Post(String content, String author, int likes) {}What has it done?
- Set up the shape of a
Post— three fields, each with a name and a type — but noPostexists yet. - Made one
Post, with emptycontentandauthorand0likes.Arecordline never fills in values. That’s whatnew Post(...)is for. - Made three definitions:
content,author, andlikes.Those are the fields of aPost. They only get values when aPostis constructed withnew. CompareString author = "tritonfan";, which defines a name and gives it a value at the same time. - Nothing: it’s an error, because the fields have no values.Not a mistake — a
recordline on its own is exactly how we describe the shape. The values arrive withnew.
Fill in a
record definition for Student so that the last line compiles and runs: a student has a name and a birth year.Student s = new Student("Val", 2004);