A class is the definition of a data structure plus actions that operate on them which typically corresponds to a real world object or concept. A class is defined once but is not used directly within an application. Instead, objects are created (instantiated) based on a class and are allocated memory.
Throughout the chapter we will illustrate the use of constructors and methods using the Employee
class. A part of this class is shown as follows:
public class Employee { private String name; private int zip; private int age; … }
This definition will be expanded to explain the concepts and techniques associated with classes and objects.
Objects are created using the new
keyword. The keyword is used in conjunction with a classname and results in memory being allocated from the heap for the object. The heap is a region of memory normally located "above" the stack as discussed in the Stack and heap section in Chapter 2, Java Data Types and Their Usage.
When a new...