OOP 2. Data - Student Class
- Classes versus objects.
- How to create objects and assign values to the instance variables (fields).
- Good practice: One class per file is the standard.
- Using
new
to create an object. - Referencing fields using dot notation.
One class per file is standard practice
Here is the class again that represents information about a student.
Typically there is one class definition per file, so this would be
stored in Student1.java
.
// File : oop/dataclass/Student1.java // Purpose: Information about a student. public class Student1 { public String firstName; // First name public String lastName; // Last name public int id; // Student id }
Class versus object
A class is a template that defines what attributes an object can have.
You make one class definition, then create objects of that class using the new
keyword.
Some analogies might make this clearer.
Analogy: Cookie cutter and cookies. A cookie cutter is like a class definition. It isn't a cookie, but can be used to create a cookie. Each cookie can will have the same attributes (shape), but each cookie can have different values for the attributes (type of dough, thickness, ...).
Analogy: Dog and Fido. The concept of Dog is like a class, but there are many possible instances of this class, eg, my dog Fido, or your dog Rover.
Analogy: Form stamp and filled out forms. There are rubber inkpad stamps that are used to stamp out a small form. These have been used in several places in my passport, where passport control stamped my passport, then filled out the fields with the date, how long I could stay, etc. The rubber stamp is like a class, defining the fields. The filled out form in each passport are the objects, with specific values for each field.
Use new
to create a new object
A class defines what fields an object will have when it's created. When a new Student1 object is created, a block of memory is allocated, which is big enough to hold these three fields -- as well as some extra overhead that all objects have.
Student1 tatiana;
tatiana = new Student1(); // Create Student1 object with new.
"new" and parentheses
To create a new object, write new
followed by the
name of the class (eg, Student1
), followed by parentheses.
Later we'll see that we can specify arguments in the parentheses when creating a new objects.
Default field values - null, zero, false
Unlike local variables in a method, fields do have default values (like the default values in arrays). Object references are null, numbers are zero, and booleans are false.
Access public fields with dot notation
The fields (firstName
, lastName
, id
)
name data which is stored in each object.
Another term for field is instance variable.
All public fields can be referenced using dot notation
(later we'll see better ways to access and set fields).
To reference a field, write the object name, then a dot, then the
field name.
Student1 tatiana; // Declare a variable to hold a Student1 object. //... Create a new Student1 object for Tatiana. tatiana = new Student1(); // Create a new Student1 object with default values. tatiana.firstName = "Tatiana"; // Set values of the fields. tatiana.lastName = "Johnson"; tatiana.id = 9950842; JOptionPane.showMessageDialog(null, "One student is named: " + tatiana.lastName + ", " + tatiana.firstName);
Awkward? This simple example with one student is somewhat awkward, but we'll get to examples that show real advantages soon.
A class definition is a template for creating objects
A class defines which fields an object has in it.
You need to use new
to create a new object from the class
by allocating memory and assigning a default value to each field.
A little later you will learn how to write a constructor to
control the initialization.
It's not very interesting to create only one object of class, so the following example creates a couple of them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// File : oop/dataclass/TestStudent1.java import javax.swing.*; public class TestStudent1 { public static void main(String[] args) { Student1 tatiana; Student1 pupil; //... Create new Student1 object with new. tatiana = new Student1(); tatiana.firstName = "Tatiana"; tatiana.lastName = "Johnson"; tatiana.id = 9950842; //... Create another Student1 object. pupil = new Student1(); pupil.firstName = JOptionPane.showInputDialog(null, "First name"); pupil.lastName = JOptionPane.showInputDialog(null, "Last name"); pupil.id = Integer.parseInt(JOptionPane.showInputDialog(null, "ID")); JOptionPane.showMessageDialog(null, "One student is named: " + tatiana.lastName + ", " + tatiana.firstName + "\n and another is named: " + pupil.lastName + ", " + pupil.firstName); } } |
Review Questions
[TODO]