7.22. Programming Exercises

 
[Page 249]

Chapter Summary

  • A class is a template for objects. It defines the generic properties of objects, and provides constructors for creating objects and methods for manipulating them.

  • A class is also a data type. You can use it to declare object reference variables . An object reference variable that appears to hold an object actually contains a reference to that object. Strictly speaking, an object reference variable and an object are different, but most of the time the distinction between them can be ignored.

  • An object is an instance of a class. You use the new operator to create an object, and the dot ( . ) operator to access members of that object through its reference variable.

  • An instance variable or method belongs to an instance of a class. Its use is associated with individual instances. A static variable is a variable shared by all instances of the same class. A static method is a method that can be invoked without using instances.

  • Every instance of a class can access the class's static variables and methods. However, it is better to invoke static variables and methods using ClassName.variable and ClassName.method for clarity.

  • Modifiers specify how the class, method, and data are accessed. A public class, method, or data is accessible to all clients . A private method or data is only accessible inside the class.

  • You can provide a get method or a set method to enable clients to see or modify the data. Colloquially, a get method is referred to as a getter (or accessor ), and a set method is referred to as a setter (or mutator ).

  • A get method has the signature public returnType getPropertyName() . If the returnType is boolean , the get method should be defined as public boolean isPropertyName() . A set method has the signature public void setPropertyName(dataType propertyValue) .

  • All parameters are passed to methods using pass- by-value . For a parameter of a primitive type, the actual value is passed; for a parameter of a reference type, the reference for the object is passed.

  • The scope of instance and static variables is the entire class, regardless of where the variables are declared. Instance and static variables can be declared anywhere in the class.

  • The keyword this can be used to refer to the calling object. It can also be used inside a constructor to invoke another constructor of the same class.

  • A Java array is an object that can contain primitive type values or object type values. When an array is created, its elements are assigned the default value of for the numeric primitive data types, '\u0000' for char types, false for boolean types, and null for object types.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net