Archive for May, 2007

426 Object-Based Programming Chapter 8 8.14 Finalizers We

Tuesday, May 8th, 2007

426 Object-Based Programming Chapter 8 8.14 Finalizers We have seen that constructor methods are capable of initializing data in an object of a class when the class is created. Often, constructors acquire various system resources such as memory (when the new operator is used). We need a disciplined way to give resources back to the system when they are no longer needed to avoid resource leaks. The most common resource acquired by constructors is memory. Java performs automatic garbage collection of memory to help return memory back to the system. When an object is no longer used in the program (i.e., there are no references to the object), the object is marked for garbage collection. The memory for such an object can be reclaimed when the garbage collector executes. Therefore, memory leaks that are common in other languages like C and C++ (because memory is not automatically reclaimed in those languages) are less likely to happen in Java. However, other resource leaks can occur. Performance Tip 8.5 Extensive use of local variables that refer to objects can degrade performance. When a local variable is the only reference to an object, the object is marked for garbage collection as the local variable goes out of scope. If this happens frequently in short time periods, large numbers of objects could be marked for garbage collection, thus placing a performance burden on the garbage collector. Every class in Java can have a finalizer method that returns resources to the system. The finalizer method for an object is guaranteed to be called to perform termination housekeeping on the object just before the garbage collector reclaims the memory for the object. A class s finalizer method always has the name finalize, receives no parameters and returns no value (i.e., its return type is void). A class should have only one finalize method that takes no arguments. Method finalize is defined originally in class Object as a placeholder that does nothing. This guarantees that every class has a finalize method for the garbage collector to call. Good Programming Practice 8.10 The last statement in a finalize method should always be super.finalize(); to ensure that the superclass s finalize method is called. Software Engineering Observation 8.20 The garbage collector is not guaranteed to execute; therefore, an object s finalize method is not guaranteed to get called. You should not architect classes that rely on the garbage collector calling an object s finalize method to deallocate resources. Finalizers have not been provided for the classes presented so far. Actually, finalizers are rarely used in industrial Java applications. We will see a sample finalize method and discuss the garbage collector further in Figure 8.20. Testing and Debugging Tip 8.4 Several professional Java developers who reviewed this book indicated that method finalize is not useful in industrial Java applications, because it is not guaranteed to get called. For this reason, you should search your Java programs for any use of method finalize to ensure that the program does not rely on calls to method finalize for proper deallocation of resources. In fact, you might consider removing method finalize entirely and Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

Chapter 8 Object-Based Programming 425 1 // Fig.

Tuesday, May 8th, 2007

Chapter 8 Object-Based Programming 425 1 // Fig. 8.19: TimeTest6.java 2 // Chaining method calls together with the this reference 3 4 // Java extension packages 5 import javax.swing.*; 6 7 // Deitel packages 8 import com.deitel.jhtp4.ch08.Time4; 9 10 public class TimeTest6 { 11 12 // test method call chaining with object of class Time4 13 public static void main( String args[] ) 14 { 15 Time4 time = new Time4(); 16 17 // chain calls to setHour, setMinute and setSecond 18 time.setHour( 18 ).setMinute( 30 ).setSecond( 22 ); 19 20 // use method call chaining to set new time and get 21 // String representation of new time 22 String output = 23 “Universal time: ” + time.toUniversalString() + 24 “nStandard time: ” + time.toString() + 25 “nnNew standard time: ” + 26 time.setTime( 20, 20, 20 ).toString(); 27 28 JOptionPane.showMessageDialog( null, output, 29 “Chaining Method Calls”, 30 JOptionPane.INFORMATION_MESSAGE ); 31 32 System.exit( 0 ); 33 } 34 35 } // end class TimeTest6 Fig. 8.19 Concatenating method calls. Note that the purpose of the example in Fig. 8.18 and Fig. 8.19 is to demonstrate the mechanics of concatenated method calls. Many Java methods return references to objects that can be used in the manner shown here. It is important to understand concatenated method calls as they appear frequently in Java programs. Good Programming Practice 8.9 For program clarity, avoid using concatenated method calls. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

424 Object-Based Programming Chapter 8 (Best web hosting) 105 // convert

Tuesday, May 8th, 2007

424 Object-Based Programming Chapter 8 105 // convert to String in standard-time format 106 public String toString() 107 { 108 DecimalFormat twoDigits = new DecimalFormat( “00″ ); 109 110 return ( this.getHour() == 12 || this.getHour() == 0 ? 111 12 : this.getHour() % 12 ) + “:” + 112 twoDigits.format( this.getMinute() ) + “:” + 113 twoDigits.format( this.getSecond() ) + 114 ( this.getHour() < 12 ? " AM" : " PM" ); 115 } 116 117 } // end class Time4 Fig. 8.18 Class Time4using thisto enable chained method calls (part 3 of 3). The example again demonstrates the explicit use of the thisreference inside the body of a class. In class Time4, every use of an instance variable of the class and every call to another method in class Time4uses the thisreference explicitly. Most programmers prefer not to use the thisreference unless it is required or helps clarify a piece of code. Good Programming Practice 8.8 Explicitly using this can increase program clarity in some contexts in which this is optional. In application class TimeTest6 (Fig. 8.19), line 18 and line 26 both demonstrate method call chaining. Why does the technique of returning the this reference work? Let us discuss line 18. The dot operator (.) associates from left to right, so the expression time.setHour( 18 ).setMinute( 30 ).setSecond( 22 ); first evaluates time.setHour(18), then returns a reference to object timeas the result of this method call. Any time you have a reference in a program (even as the result of a method call), the reference can be followed by a dot operator and a call to one of the methods of the reference type. Thus, the remaining expression is interpreted as time.setMinute( 30 ).setSecond( 22 ); The time.setMinute( 30 ) call executes and returns a reference to time. The remaining expression is interpreted as time.setSecond( 22 ); When the statement is complete, the time is 18for the hour, 30for the minuteand 22 from the second. Note that the calls on line 26 time.setTime( 20, 20, 20 ).toString(); also use the concatenation feature. These method calls must appear in this order in this expression because toStringas defined in the class does not return a reference to a Time4 object. Placing the call to toString before the call to setTime causes a syntax error. Note that toStringreturns a reference to a Stringobject. Therefore, a method of class Stringcould be concatenated to the end of line 26. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

Chapter 8 Object-Based Programming 423 53 this.setMinute( minute (Photo web hosting)

Monday, May 7th, 2007

Chapter 8 Object-Based Programming 423 53 this.setMinute( minute ); // set minute 54 this.setSecond( second ); // set second 55 56 return this; // enables chaining 57 } 58 59 // validate and set hour 60 public Time4 setHour( int hour ) 61 { 62 this.hour = ( hour >= 0 && hour < 24 ? hour : 0 ); 63 64 return this; // enables chaining 65 } 66 67 // validate and set minute 68 public Time4 setMinute( int minute ) 69 { 70 this.minute = 71 ( minute >= 0 && minute < 60 ) ? minute : 0; 72 73 return this; // enables chaining 74 } 75 76 // validate and set second 77 public Time4 setSecond( int second ) 78 { 79 this.second = 80 ( second >= 0 && second < 60 ) ? second : 0; 81 82 return this; // enables chaining 83 } 84 85 // Get Methods 86 // get value of hour 87 public int getHour() { return this.hour; } 88 89 // get value of minute 90 public int getMinute() { return this.minute; } 91 92 // get value of second 93 public int getSecond() { return this.second; } 94 95 // convert to String in universal-time format 96 public String toUniversalString() 97 { 98 DecimalFormat twoDigits = new DecimalFormat( "00" ); 99 100 return twoDigits.format( this.getHour() ) + ":" + 101 twoDigits.format( this.getMinute() ) + ":" + 102 twoDigits.format( this.getSecond() ); 103 } 104 Fig. 8.18 Class Time4using thisto enable chained method calls (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

422 Object-Based Programming Chapter 8 1 // Fig.

Monday, May 7th, 2007

422 Object-Based Programming Chapter 8 1 // Fig. 8.18: Time4.java 2 // Time4 class definition 3 package com.deitel.jhtp4.ch08; 4 5 // Java core packages 6 import java.text.DecimalFormat; 7 8 public class Time4 extends Object { 9 private int hour; // 0 - 23 10 private int minute; // 0 - 59 11 private int second; // 0 - 59 12 13 // Time4 constructor initializes each instance variable 14 // to zero. Ensures that Time object starts in a 15 // consistent state. 16 public Time4() 17 { 18 this.setTime( 0, 0, 0 ); 19 } 20 21 // Time4 constructor: hour supplied, minute and second 22 // defaulted to 0 23 public Time4( int hour ) 24 { 25 this.setTime( hour, 0, 0 ); 26 } 27 28 // Time4 constructor: hour and minute supplied, second 29 // defaulted to 0 30 public Time4( int hour, int minute ) 31 { 32 this.setTime( hour, minute, 0 ); 33 } 34 35 // Time4 constructor: hour, minute and second supplied 36 public Time4( int hour, int minute, int second ) 37 { 38 this.setTime( hour, minute, second ); 39 } 40 41 // Time4 constructor: another Time4 object supplied. 42 public Time4( Time4 time ) 43 { 44 this.setTime( time.getHour(), time.getMinute(), 45 time.getSecond() ); 46 } 47 48 // Set Methods 49 // set a new Time value using universal time 50 public Time4 setTime( int hour, int minute, int second ) 51 { 52 this.setHour( hour ); // set hour Fig. 8.18 Class Time4using thisto enable chained method calls (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

Adult web hosting - Chapter 8 Object-Based Programming 421 Fig. 8.17 Using

Monday, May 7th, 2007

Chapter 8 Object-Based Programming 421 Fig. 8.17 Using the thisreference implicitly and explicitly (part 3 of 3). Class SimpleTime(lines 8 60) defines three privateinstance variables hour, minuteand second. The constructor (lines 34 39) receives three intarguments to initialize a SimpleTimeobject. Notice that the parameter names for the constructor are the same as the instance variable names. Remember that a local variable of a method with the same name as an instance variable of a class hides the instance variable in the scope of the method. For this reason, we use the thisreference to refer explicitly to the instance variables on lines 36 38. Common Programming Error 8.11 In a method in which a method parameter has the same name as one of the class members, use this explicitly if you want to access the class member; otherwise, you will incorrectly reference the method parameter. Good Programming Practice 8.7 Avoid using method parameter names that conflict with class member names. Method buildString (lines 43 48) returns a String created with a statement that uses the this reference three ways. Line 45 explicitly invokes the class s toString method via this.toString(). Line 46 implicitly uses the this reference to perform the same task. Line 47 appends thisto the string that will be returned. Remember that the this reference is a reference to an object the current Simple- Timeobject being manipulated. As before, any reference added to a String results in a call to the toString method for the referenced object. Method buildString is invoked at line 18 to display the results of the three calls to toString. Note that the same time is displayed on all three lines of the output because all three calls to toStringare for the same object. Another use of the this reference is in enabling concatenated method calls (also called cascaded method calls or method call chaining). Figure 8.18 illustrates returning a reference to a Time4object to enable method calls of class Time4to be concatenated. Methods setTime (lines 50 57), setHour (lines 60 65), setMinute (line 68 74) and setSecond(lines 77 83) each have a return type of Time4and each has as its last statement return this; to indicate that a reference to the Time4 object being manipulated should be returned to the caller of the method. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

420 Object-Based Programming Chapter 8 (How to cite a web site) 11 public class

Sunday, May 6th, 2007

420 Object-Based Programming Chapter 8 11 public class ThisTest { 12 13 // test class SimpleTime 14 public static void main( String args[] ) 15 { 16 SimpleTime time = new SimpleTime( 12, 30, 19 ); 17 18 JOptionPane.showMessageDialog( null, time.buildString(), 19 “Demonstrating the “this” Reference”, 20 JOptionPane.INFORMATION_MESSAGE ); 21 22 System.exit( 0 ); 23 } 24 25 } // end class ThisTest 26 27 // class SimpleTime demonstrates the “this” reference 28 class SimpleTime { 29 private int hour, minute, second; 30 31 // constructor uses parameter names identical to instance 32 // variable names, so “this” reference required to distinguish 33 // between instance variables and parameters 34 public SimpleTime( int hour, int minute, int second ) 35 { 36 this.hour = hour; // set “this” object’s hour 37 this.minute = minute; // set “this” object’s minute 38 this.second = second; // set “this” object’s second 39 } 40 41 // call toString explicitly via “this” reference, explicitly 42 // via implicit “this” reference, implicitly via “this” 43 public String buildString() 44 { 45 return “this.toString(): ” + this.toString() + 46 “ntoString(): ” + toString() + 47 “nthis (with implicit toString() call): ” + this; 48 } 49 50 // convert SimpleTime to String format 51 public String toString() 52 { 53 DecimalFormat twoDigits = new DecimalFormat( “00″ ); 54 55 // “this” not required, because toString does not have 56 // local variables with same names as instance variables 57 return twoDigits.format( this.hour ) + “:” + 58 twoDigits.format( this.minute ) + “:” + 59 twoDigits.format( this.second ); 60 } 61 62 } // end class SimpleTime Fig. 8.17 Using the thisreference implicitly and explicitly (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services

Web hosting - Chapter 8 Object-Based Programming 419 54 } //

Sunday, May 6th, 2007

Chapter 8 Object-Based Programming 419 54 } // end class PackageData Fig. 8.16 Package access to members of a class (part 2 of 2). Software Engineering Observation 8.19 Some people in the OOP community feel that package access corrupts information hiding and weakens the value of the object-oriented design approach, because the programmer must assume responsibility for error checking and data validation in any code that manipulates the package access data members. 8.13 Using the this Reference When a method of a class references another member of that class for a specific object of that class, how does Java ensure that the proper object is referenced? The answer is that each object has access to a reference to itself called the this reference. The this reference is used implicitly to refer to both the instance variables and methods of an object. We begin with a simple example of using the thisreference explicitly; a subsequent example shows some substantial and subtle examples of using this. Performance Tip 8.4 Java conserves storage by maintaining only one copy of each method per class; this method is invoked by every object of that class. Each object, on the other hand, has its own copy of the class s instance variables. The application of Fig. 8.17 demonstrates implicit and explicit use of the thisreference to enable the main method of class ThisTest to display the private data of a SimpleTimeobject. 1 // Fig. 8.17: ThisTest.java 2 // Using the this reference to refer to 3 // instance variables and methods. 4 5 // Java core packages 6 import java.text.DecimalFormat; 7 8 // Java extension packages 9 import javax.swing.*; 10 Fig. 8.17 Using the thisreference implicitly and explicitly (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for cheap webhost to host and run your apache application check Vision jboss web hosting services

418 Object-Based Programming Chapter 8 1 (Unable to start debugging on the web server) // Fig.

Sunday, May 6th, 2007

418 Object-Based Programming Chapter 8 1 // Fig. 8.16: PackageDataTest.java 2 // Classes in the same package (i.e., the same directory) can 3 // use package access data of other classes in the same package. 4 5 // Java extension packages 6 import javax.swing.JOptionPane; 7 8 public class PackageDataTest { 9 10 // Java extension packages 11 public static void main( String args[] ) 12 { 13 PackageData packageData = new PackageData(); 14 15 // append String representation of packageData to output 16 String output = 17 “After instantiation:n” + packageData.toString(); 18 19 // change package access data in packageData object 20 packageData.number = 77; 21 packageData.string = “Good bye”; 22 23 // append String representation of packageData to output 24 output += “nAfter changing values:n” + 25 packageData.toString(); 26 27 JOptionPane.showMessageDialog( null, output, 28 “Demonstrating Package Access”, 29 JOptionPane.INFORMATION_MESSAGE ); 30 31 System.exit( 0 ); 32 } 33 34 } // end class PackageDataTest 35 36 // class with package access instance variables 37 class PackageData { 38 int number; // package access instance variable 39 String string; // package access instance variable 40 41 // constructor 42 public PackageData() 43 { 44 number = 0; 45 string = “Hello”; 46 } 47 48 // convert PackageData object to String representation 49 public String toString() 50 { 51 return “number: ” + number + ” string: ” + string; 52 } 53 Fig. 8.16 Package access to members of a class (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

Chapter 8 Object-Based Programming 417 18 JOptionPane.showMessageDialog( null,

Sunday, May 6th, 2007

Chapter 8 Object-Based Programming 417 18 JOptionPane.showMessageDialog( null, 19 employee.toString(), “Testing Class Employee”, 20 JOptionPane.INFORMATION_MESSAGE ); 21 22 System.exit( 0 ); 23 } 24 25 } // end class EmployeeTest Date object constructor for date 7/24/1949 Date object constructor for date 3/12/1988 Fig. 8.15 Demonstrating an object with a member object reference (part 2 of 2). 8.12 Package Access When no member access modifier is provided for a method or variable when it is defined in a class, the method or variable is considered to have package access. If your program consists of one class definition, this has no specific effects on the program. However, if your program uses multiple classes from the same package (i.e., a group of related classes), these classes can access each other s package-access methods and data directly through a reference to an object. Performance Tip 8.3 Package access enables objects of different classes to interact without the need for set and get methods that provide access to data, thus eliminating some of the method call overhead. Let us consider a mechanical example of package access. The application of Fig. 8.16 contains two classes the PackageDataTest application class (lines 8 34) and the PackageData class (lines 37 54). In the PackageData class definition, lines 38 39 declare the instance variables number and string with no member access modifiers; therefore, these are package access instance variables. The PackageDataTestapplication s main method creates an instance of the PackageData class (line 13) to demonstrate the ability to modify the PackageData instance variables directly (as shown on lines 20 21). The results of the modification can be seen in the output window. When you compile this program, the compiler produces two separate files a .class file for class PackageData and a .class file for class PackageDataTest. Every Java class has its own .classfile. These two .classfiles are placed in the same directory by the compiler automatically and are considered to be part of the same package (they are certainly related by the fact that they are in the same file). Because they are part of the same package, class PackageDataTest is allowed to modify the package access data of objects of class PackageData. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services