126 Introduction to Java Applets Chapter 3 Software (Web design company)

126 Introduction to Java Applets Chapter 3 Software Engineering Observation 3.5 Many packages have subpackages. For example, the java.awt package has subpackage event for the package java.awt.event. When the compiler encounters an import statement that uses the * (e.g., java.awt.*) notation to indicate that a program uses multiple classes from a package, the compiler does not load classes from the subpackage event. Thus, you cannot define an import of java.* to search for classes from all Java core packages. Software Engineering Observation 3.6 When using import statements, separate import statements must be specified for each package used in a program. Common Programming Error 3.6 Assuming that an import statement for an entire package (e.g., java.awt.*) also imports classes from subpackages in that package (e.g., java.awt.event.*) results in syntax errors for the classes from the subpackages. There must be separate import statements for every package from which classes are used. Remember that applets inherit from the JApplet class, so they have all the methods that an applet container requires to execute the applet. Line 10 public class AdditionApplet extends JApplet { begins class AdditionApplet s definition and indicates that it inherits from JApplet. All class definitions start with an opening left brace (end of line 10), {, and end with a closing right brace, } (line 52). Common Programming Error 3.7 If braces do not occur in matching pairs, the compiler indicates a syntax error. Line 11 double sum; // sum of values entered by user is an instance variable declaration every instance (object) of the class contains one copy of each instance variable. For example, if there are 10 instances of this applet executing, each instance has its own copy of sum. Thus, there would be 10 separate copies of sum (one per applet). Programmers declare instance variables in the body of the class definition, but outside the bodies of all the class s method definitions. The preceding declaration states that sumis a variable of primitive type double. A benefit of instance variables is that all the methods of the class can use the instance variables. Until now, we declared all variables in an application s main method. Variables defined in the body of a method are known as local variables and can be used only in the body of the method in which they are defined. Another distinction between instance variables and local variables is that instance variables have default values and local variables do not. The default value of variable sum is 0.0, because sum is an instance variable. Good Programming Practice 3.3 Explicitly initializing instance variables rather than relying on automatic initialization improves program readability. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply