Classes and Objects Part 2 Static Class Members and Arrays of Objects PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Classes and Objects Part 2 Static Class Members and Arrays of Objects


1
Classes and Objects Part 2Static Class
MembersandArrays of Objects
  • Corresponds with Chapter 7

2
Class (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.

3
Making 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.

4
Static 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.

5
CircleWithStaticVariableAndMethod class (similar
to Listing 7.5 p242)
6
UML Diagram
Static members are underlined in UML
7
Application using the CircleWithStaticVariableAndM
ethod class
8
At 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
9
Object is instantiated
Frame Stack
Heap
10
Default constructor starts to execute
Frame Stack
Heap
11
Frame Stack
Heap
12
Frame Stack
Heap
13
Constructor terminates and address is assigned
into reference
Frame Stack
Heap
14
2nd object is instantiated
Frame Stack
Heap
15
The 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
18
Constructor terminates and address is assigned
into reference
Frame Stack
Heap
19
Arrays of Objects
  • Array elements are references
  • So, there is a reference that points to the
    array, and the array in turn points to objects.

20
Application using an array of Circle Objects
similar to Listing 7.10 pp251-252
21
Create the local variablea reference to an array
of circle objects. NOT an array. NOT a circle
object.
Frame Stack
Heap
22
Method call. Frame goes on stack.
create Circle Arrays frame
Frame Stack
Heap
23
Instantiate 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
25
Constructor terminates. Return value of new is
assigned into array element.
create Circle Arrays frame
0
0
1
2
9
Frame Stack
Heap
26
newRadius
28.4
Instantiate another Circle object. Constructor is
invoked.
create Circle Arrays frame
0
1
2
9
Frame Stack
Heap
27
Constructor terminates. Return value of new is
assigned into next array element.
create Circle Arrays frame
1
0
1
2
9
Frame Stack
Heap
28
Eventually, array is filled. Array reference gets
returned. Return value is assigned into mains
local variable.
0
1
2
9
Frame Stack
Heap
Write a Comment
User Comments (0)
About PowerShow.com