CS151 lecture notes, Fall 1999 Week 9, Monday Quiz 11 Summary of Chapter 8 Reasons objects are good ------------------------ 1) Chunk related information together. Chunking overcomes human cognitive limitations. 2) Share information between methods in controlled ways. 3) Return more than one value from a method. Dangers of objects ------------------ 1) Don't forget that when you create an object variable, you don't get an object. 2) Beware the NullPointerException! 3) For primitive local variables, methods cannot interfere with each other -- they are encapsulated. Objects undermine encapsulation, allowing methods to interact in complex ways that can be confusing. Intro to Chapter 9 So far, a class is a collection of related methods. (Math contains the math methods, Slate contains the Slate methods) Also, a class specifies the instance variables of an object type (Point indicates that Points contain two integers, Rectangles four) Together, a class specifies the instance variables of an object type, and the methods that pertain to that type. Creating your own object types ------------------------------ 1) you've already done it. Every time you write a new class definition, you create a new object type with the same name On day 1, you created a new object type named cs151. You could have created a variable with type cs151. It's usually not useful to do that because a cs151 object would not have had any instance variables. 2) How do you specify the instance variables? They go right after the first line of the class definition, before any of the methods. public class Time { int hour, minute; double second; // method definitions go here } Every time you create a Time object, it will have a copy of the instance variables. 3) Once you define a new class, you can create new variables with that type: public static void main (String[] args) { Time fred; fred = new Time (); fred.hour = 17; fred.second = 3.14159; } 4) Usually you want to put all the methods associated with an object into the class definition for that object. Second example: A complex number consists of a real part and an imaginary part, both doubles: public class Complex { double real, imag; public static void main (String[] args) { Complex c = new Complex (); c.real = 1.0; c.imag = 2.0; } } Constructors ------------ When you create a new object, you usually want to initialize the instance variables. Constructors are a special kind of method that does that. For the Time class, a constructor might look like: public Time () { this.hour = 0; this.minute = 0; this.second = 0.0; } Notice the weird syntax. 1) no static 2) no return type 3) no return statement 4) "this" is a Java keyword that indicates "the current object" Write a constructor for Complex... How do constructors get invoked? -------------------------------- When you invoke the new command, Java allocates space for your object and then invokes your constructor. Your constructor initializes the instance variables. Inside the constructor, the name of the new object is "this" Your constructor does not return a reference to "this". When your constructor returns, flow of execution resumes from the point of the new command. Summary ------- Every object is an instance of some object type. Every object type is defined by a class definition. The class definition specifies the names and types of the instance variables. You can provide one or more constructor methods, whose job is to initialize the instance variables.