Chapter 8 Object-Based (Medical web site) Programming 413 8 // Java
Chapter 8 Object-Based Programming 413 8 // Java extension packages 9 import javax.swing.*; 10 11 public class Increment extends JApplet 12 implements ActionListener { 13 14 private int count = 0, total = 0; 15 private final int INCREMENT = 5; // constant variable 16 17 private JButton button; 18 19 // set up GUI 20 public void init() 21 { 22 Container container = getContentPane(); 23 24 button = new JButton( “Click to increment” ); 25 button.addActionListener( this ); 26 container.add( button ); 27 } 28 29 // add INCREMENT to total when user clicks button 30 public void actionPerformed( ActionEvent actionEvent ) 31 { 32 total += INCREMENT; 33 count++; 34 showStatus( “After increment ” + count + 35 “: total = ” + total ); 36 } 37 38 } // end class Increment Fig. 8.11Initializing a finalvariable (part 2 of 2). Fig. 8.11 Figure 8.12 illustrates compiler errors produced for the program of Fig. 8.11 if instance variable INCREMENTis declared final, but is not initialized in the declaration. Increment.java:11: variable INCREMENT might not have been initialized public class Increment extends JApplet ^ 1 error Fig. 8.1212 Compiler error message as a result of not initializing increment. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01