Title: Classes and Objects Part 2 Static Class Members and Arrays of Objects
1Classes and Objects Part 2Static Class
MembersandArrays of Objects
- Corresponds with Chapter 7
2Class (static) Member Variables
- Recall that with instance variables, there is one
variable for each instance (object) of the class
that is created. - With class variables, there is only a single
variable for the entire class, not one for each
instance. - Class variables are declared using the static
modifier. - Class variables can be accessed through an
instance, or just through the class - instanceName.memberName OR
- className.memberName (this is preferable)
- Class member variables are useful if you want to
share the same data across all instances of a
class.
3Making Constants
- Using the static and final modifiers, you can
create a constant - When using the final modifier, you must
initialize the member variable upon declaration. - After initializing a final member, you can never
assign another value to it.
4Static Methods
- Static (class) methods operate on the entire
class - These methods have no this pointerthus they
cannot access instance members without respect to
a particular instance. They can only access other
static members. - For example, the main method is a static method.
It is not called with respect to a particular
instance.
5CircleWithStaticVariableAndMethod class (similar
to Listing 7.5 p242)
6UML Diagram
Static members are underlined in UML
7Application using the CircleWithStaticVariableAndM
ethod class
8At start of application, when CircleWithStaticVari
ableAndMethod class is first loaded, its static
member variable is created and initializedthis
is prior to and independent of any creation of
instances
Frame Stack
Heap
9Object is instantiated
Frame Stack
Heap
10Default constructor starts to execute
Frame Stack
Heap
11Frame Stack
Heap
12Frame Stack
Heap
13Constructor terminates and address is assigned
into reference
Frame Stack
Heap
142nd object is instantiated
Frame Stack
Heap
15The other constructor starts to execute
newRadius
5.0
Frame Stack
Heap
16 constructor frame
newRadius
5.0
Frame Stack
Heap
17 constructor frame
newRadius
5.0
Frame Stack
Heap
18Constructor terminates and address is assigned
into reference
Frame Stack
Heap
19Arrays of Objects
- Array elements are references
- So, there is a reference that points to the
array, and the array in turn points to objects.
20Application using an array of Circle Objects
similar to Listing 7.10 pp251-252
21Create the local variablea reference to an array
of circle objects. NOT an array. NOT a circle
object.
Frame Stack
Heap
22Method call. Frame goes on stack.
create Circle Arrays frame
Frame Stack
Heap
23Instantiate the array (not the Circle objects).
create Circle Arrays frame
Frame Stack
Heap
24 constructor frame
newRadius
52.3
Instantiate a Circle object. Constructor is
invoked.
create Circle Arrays frame
0
0
1
2
9
Frame Stack
Heap
25Constructor terminates. Return value of new is
assigned into array element.
create Circle Arrays frame
0
0
1
2
9
Frame Stack
Heap
26newRadius
28.4
Instantiate another Circle object. Constructor is
invoked.
create Circle Arrays frame
0
1
2
9
Frame Stack
Heap
27Constructor terminates. Return value of new is
assigned into next array element.
create Circle Arrays frame
1
0
1
2
9
Frame Stack
Heap
28Eventually, array is filled. Array reference gets
returned. Return value is assigned into mains
local variable.
0
1
2
9
Frame Stack
Heap