CS151 lecture notes, Fall 1999 Week 9, Wednesday Important ideas from last time: 1) Every time you create a new class, you create a new object type with the same name. 2) You can specify the instance variables for the new objects by declaring them outside of any method. Examples: public class Time { int hour, minute; double second; // method definitions go here } public class Complex { double real, imag; // method definitions go here } Instance variables are global, meaning that they can be accessed (read/written) from any method in the class (but not from methods in other classes). Although you declare instance variables outside any method, you usually initialize them in a special method called a constructor. 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. Example: public static void main (String[] args) { Time fred; fred = new Time (); fred.hour = 17; fred.second = 3.14159; } How would we create a complex object? Constructors with parameters ---------------------------- It is very common to provide more than one constructor for a class, each of which takes different parameters. In other words, constructors can be overloaded, just like other methods. Usually there is one constructor that takes no parameters and that initializes the instance variables to default values. Usually there is a second constructor that takes a list of parameters identical to the list of instance variables, and that uses the values of the parameters to initialize the instance variables. Examples: public Time (int hour, int minute, double second) { this.hour = hour; this.minute = minute; this.second = second; } public Complex (double real, double imag) { this.real = real; this.imag = imag; } Now you can create a new object and initialize the instance variables at the same time: public static void main (String[] args) { Time fred = new Time (11, 29, 3.14159); Complex c = new Complex (1.0, 2.0); } 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 between a constructor and a method that happens to return the object type in question? 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.