03 - 19:29:17 CS 61B Lecture 3 Wednesday September 1 2010...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
09/01/10 19:29:17 1 03 CS 61B: Lecture 3 Wednesday, September 1, 2010 Today’s reading: Sierra & Bates, pp. 71-74, 76, 85, 240-249, 273-281, 308-309. DEFINING CLASSES ================ An object is a repository of data. _Fields_ are variables that hold the data stored in objects. Fields in objects are also known as _instance_variables_. In Java, fields are addressed much like methods are, but fields never have parameters, and no parentheses appear after them. For example, suppose that kayla is a Human object. Then kayla.introduce() is a method call, and kayla.age is a field. Let’s write a _class_definition_ for the Human class. class Human { public int age; // The Human’s age (an integer). public String name; // The Human’s name. public void introduce() { // This is a _method_definition_. System.out.println("I’m " + name + " and I’m " + age + " years old."); } } "age" and "name" are both fields of a Human object. Now that we’ve defined the Human class, we can construct as many Human objects as we want. Each Human object we create can have different values of age and name. We might create kayla by executing the following code. Human kayla = new Human(); // Create kayla. kayla.age = 12; // Set kayla’s fields. kayla.name = "Kayla"; kayla.introduce(); // _Method_call_ has kayla introduce herself. -------------- | ---- | --- | age |12| | kayla |.+--->| ---- | ----------- --- | name | -+--+---->| "Kayla" | | ---- | ----------- -------------- a String object a Human object The output is: I’m Kayla and I’m 12 years old. Why is it that, inside the definition of introduce(), we don’t have to write "kayla.name" and "kayla.age"? When we invoke "kayla.introduce()", Java remembers that we are calling introduce() _on_ the object that "kayla" references. The methods defined inside the Human class remember that we’re referring to kayla’s name and age. If we had written "rishi.introduce()", the introduce method would print rishi’s name and age instead.
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern