Contrasting instance with class members
An object can be more correctly termed an object instance. This is where instance members (methods/data) get their names: every object gets a copy of an instance member. Class members, however, are different in that there is only one copy per class, regardless of the number of object instances created. We’ll discuss both of these topics now.
Instance members (methods/data)
This is more easily explained by presenting a code example first. Figure 8.3 presents a class with instance members:
Figure 8.3 – A class with instance members
When you create an object using new
, you are creating an object instance. Each instance gets a copy of the instance members (variables and methods). Regarding instance variables, we need to define where instance variables are declared and their resultant scope. An instance variable is defined within the class but outside every method coded in the class. Thus, the scope...