CS115 lecture notes, Spring 1999 Week 10, Monday Read Chapter 9! Exam Friday! 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 Hello. You could have created a variable with type Hello. It's usually not useful to do that because a Hello 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 { public int hour, minute; public double second; // method definitions go here } Notice the reappearance of our favorite word! 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 in this space... 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. Other kinds of methods ---------------------- 1) pure functions 2) modifiers 3) fill-in methods Pure function: return value depends only on the arguments (not on any other "state"), and the method does not modify any values (except local variables). Also, no side-effects, like printing something or writing a file, etc. Return value may be a primitive or object type. Examples: findCenter, distance Return value may be the same type as the operands, in which case there is sometimes confusion with constructors. How can you tell the difference? Modifier: Changes one or more fields in one or more of the objects that are passed. Usually a void method, although sometimes the return value is an error code. Fill-in method: really a special case of the modifier. One of the arguments is an "empty" object. The method puts values into it.