Initialization & Cleanup Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig
What Needs to be Initialized? A name for a student A Socket needs to have its IP address set. A Rectangle needs to have its dimensions and location set. Etc.
What If We Forget? Things dont act the way we expect them to! We only learn about problems at runtime. Maybe we dont find out until its too late. Common culprits: references that lead nowhere garbage values
How Does Java Help? Java initializes all class member variables to zero whenever we create an object. Java allows us to write constructors, special methods, one of which will be called on object creation. Java refuses to create an object (compile error) if we havent provided the right kind of constructor.
Constructors A constructor method has the same name as the class. It has no return type. There can be many different constructors, each with a distinct argument signature. (This implies that overloaded methods are OK in Java.) You specify the particular constructor you want when you create an object.
Example Constructor class Book { String title; String author; int numPages; Book() { }// default constructor Book(String t, String a, int p) { title = t; author = a; numPages = p; }
Making Books Book uselessBook = new Book(); title is an empty character sequence author is an empty character sequence numPages is 0 Book usefulBook = new Book(The TeXBook, Donald Knuth, 483);
Method Overloading Methods with the same name, but different sets of arguments. A natural idea (Wash the car? Wash the shirt? Wash the dog? …. etc) Constructors can be overloaded; so can any function. This is OK, but not recommended: void print(String s, int i) void print(int i, String s) You cant overload on the return type alone.
Overloading With Primitives The compiler tries to find an exact match, but will promote (widen) a value if necessary. The compiler wont narrow without an explicit cast. void doSomething(long l) { // whatever } : int i = 13; doSomething(i);
The Default Constructor But, how come I didnt have to write constructors for the last homework? The compiler will write one for you! But only if you havent written any constructors at all (for this class). A default constructor has no arguments (but still has the same name as the class).
A Common Error The compiler gives an error. Normally, you always provide a default constructor that does as much as possible (but not too much!). class Book { String title; String author; int numPages; Book(String t, String a, int n) { title = t; author = a, numPages = n; } : Book b = new Book();
The this Keyword If we want to send a message to an object, so in Java we say f.play(4); The compiler knows which object (f in this case) the method is being called for. The compiler sends this information to the method, in the form of a reference to f.
The this Keyword (cont.) If necessary, we can get a reference to the current object; its called this. public class Leaf { int i = 0; Leaf increment() { i++; return this; } void print() { System.out.println(i = + i); } public static void main(String[] args) { Leaf x = new Leaf(); x.increment().increment().increment().print(); }
Other Uses of this public class Flower { int petalCount = 0; String s = new String(null); Flower(int petals) { petalCount = petals; } Flower(String ss) { s = ss; } Flower(String s, int petals) { this(petals); //! this(s); // cant do it twice this.s = s; } Flower() { this(hi, 47); }// default constructor }
So, What Is A static Method? Its a method that belongs to the class but not to any instance. Static methods cannot use this keyword. You cant access non-static methods or variable from within a static method. They must use an object reference. You can call a static method without knowing any object of the class.
Cleanup Java has a garbage collector that reclaims memory. If an object cant be reached by a chain of references from a reference, it is garbage and will be removed from memory.
Member Initialization Unitialized variables are a common source of bugs. Using an unititialized variable in method gets a compiler error. Primitive data members in classes automatically get initialized to zero. Is the initialized value (zero) any better than a garbage value?
Member Initialization (cont.) You can initialize in a class definition: class Notebook { long ram = ; String name = new String(IBM); float price = ; Battery bat = new Battery(); Disk d; // a null reference int i = f(); : }
Constructors Again You can have both class initialization and constructor initialization: The order of initialization follows the order of the initialization statements in the class definition. Its done before any constructor initialization, so it may be done twice (as Counter illustrates). class Counter { int i = 1; Counter() { i = 7; } Counter(int j) { }; :
Static Member Initialization Same story; primitives get zero unless initialized, references get null unless initialized. Static initialized either when the first object of the type is created, or at the time of the first use of the variable. If you never use it, its never initialized.
Final Means that the value cannot be modified If a variable was defined as final, its initialization should be either in: The Definition statement Or, in each constructor Final static: like defining a constant in C++. It should be initialized on definition time.
Add toString() to Class A class A { int i; int j; public String toString() { return new String(I = " + i +, j = + j); //Or // return + i; // but not: // return i; }
Example of a Simple Time Class public class Time { int hour; int minute; int second; Time() { setTime(0, 0, 0); } Time(int h) { setTime(h, 0, 0); } Time(int h, int m) { setTime(h, m, 0); } Time(int h, int m, int s) { setTime(h, m, s); }
Time Class (cont.) Time setTime(int h, int m, int s) { setHour(h); setMinute(m); setSecond(s); return this; } Time setHour(int h) { hour = (( h >= 0 && h < 24 ) ? h : 0 ); return this; }
Time Class (cont.) Time setMinute(int m) { minute = (( m >= 0 && m < 60 ) ? m : 0 ); return this; } Time setSecond(int s) { second = ((s >= 0 && s < 24 ) ? s : 0 ); return this; } int getHour() { return hour; } int getMinute() { return minute; } int getSecond() { return second; }
Time Class (cont.) public String toString() { return (( hour == 12 || hour == 0 ) ? 12 : hour % 12 ) + ":" + ( minute < 10 ? "0" : "" ) + minute + ":" + ( second < 10 ? "0" : "" ) + second + ( hour < 12 ? " AM" : " PM" ) ; }
Time Class Driver public class TestTime { public static void main(String[] args) { Time t1 = new Time(); Time t2 = new Time(20, 3, 45); t1.setHour(7).setMinute(32).setSecond(23); System.out.println("t1 is " + t1); System.out.println("t2 is " + t2); }