438 Object-Based Programming Chapter 8 A constructor
438 Object-Based Programming Chapter 8 A constructor is a method with the exact same name as the class that initializes the instance variables of an object of the class when the object is instantiated. Constructor methods can be overloaded for a class. Constructors can take arguments but cannot specify a return value type. Constructors and other methods that change instance variable values should always maintain objects in a consistent state. Method toString takes no arguments and returns a String. The original toStringmethod of class Objectis a placeholder that is normally redefined by a subclass. When an object is instantiated, operator new allocates the memory for the object, then new calls the constructor for the class to initialize the instance variables of the object. If the .class files for the classes used in a program are in the same directory as the class that uses them, import statements are not required. Concatenating a String and any object results in an implicit call to the object s toString method to convert the object to a String, then the Strings are concatenated. Within a class s scope, class members are accessible to all of that class s methods and can be referenced simply by name. Outside a class s scope, class members can only be accessed off a handle (i.e., a reference to an object of the class). If a method defines a variable with the same name as a variable with class scope, the class-scope variable is hidden by the method-scope variable in the method. A hidden instance variable can be accessed in the method by preceding its name with the keyword this and the dot operator. Each class and interface in the Java API belongs to a specific package that contains a group of related classes and interfaces. Packages are actually directory structures used to organize classes and interfaces. Packages provide a mechanism for software reuse and a convention for unique class names. Creating a reusable class requires: defining a public class, adding a package statement to the class definition file, compiling the class into the appropriate package directory structure and importing the class into a program. When compiling a class in a package, the option -d must be passed to the compiler to specify where to create (or locate) all the directories in the package statement. The package directory names become part of the class name when the class is compiled. Use this fully qualified name in programs or import the class and use its short name (the name of the class by itself) in the program. If no constructors are defined for a class, the compiler creates a default constructor. When one object of a class has a reference to another object of the same class, the first object can access all the second object s data and methods. Classes often provide public methods to allow clients of the class to set (i.e., assign values to) or get (i.e., obtain the values of) private instance variables. Get methods are also commonly called accessor methods or query methods. Set methods are also commonly called mutator methods (because they typically change a value). Every event has a source the GUI component with which the user interacted to signal the program to do a task. Use the keyword finalto specify that a variable is not modifiable and that any attempt to modify the variable is an error. A finalvariable cannot be modified by assignment after it is initialized. Such a variable must be initialized in its declaration or in every constructor of the class. With composition, a class has references to objects of other classes as members. 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. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01