Download presentation
Presentation is loading. Please wait.
Published byUtami Hardja Modified over 6 years ago
1
Classes and Objects Part 2 Static Class Members and Arrays 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 pointer…thus 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
Creating a static variable
CircleWithStaticVariableAndMethod class (similar to Listing 7.5 p242) Creating a static variable A static method
6
UML Diagram Static members are underlined in UML
7
Application using the CircleWithStaticVariableAndMethod class
Using instance.member syntax for accessing static member Using classname.member syntax for accessing static member
8
At start of application, when CircleWithStaticVariableAndMethod class is first loaded, its static member variable is created and initialized…this is prior to and independent of any creation of instances CircleWithStaticVariableAndMethod class (not instance) with static data numOfObjects main’s frame args circle1 circle2 Frame Stack Heap
9
Frame Stack Heap Object is instantiated
CircleWithStaticVariable class (not instance) with static data numOfObjects CircleWithStaticVariables object radius main’s frame args circle1 circle2 Frame Stack Heap
10
Frame Stack Heap Default constructor starts to execute
CircleWithStaticVariable class (not instance) with static data numOfObjects this constructor frame CircleWithStaticVariables object radius main’s frame args circle1 circle2 Frame Stack Heap
11
CircleWithStaticVariable class (not instance) with static data
numOfObjects Instance variable is set this constructor frame CircleWithStaticVariables object radius 1.0 main’s frame args circle1 circle2 Frame Stack Heap
12
CircleWithStaticVariable class (not instance) with static data
numOfObjects 1 Static variable is set this Circle constructor frame CircleWithStaticVariables object radius 1.0 main’s frame args circle1 circle2 Frame Stack Heap
13
Constructor terminates and address is assigned into reference
CircleWithStaticVariable class (not instance) with static data numOfObjects 1 CircleWithStaticVariables object radius 1.0 main’s frame args circle1 circle2 Frame Stack Heap
14
Frame Stack Heap 2nd object is instantiated
CircleWithStaticVariable class (not instance) with static data numOfObjects 1 CircleWithStaticVariables object radius 1.0 main’s frame args circle1 CircleWithStaticVariables object radius circle2 Frame Stack Heap
15
Frame Stack Heap The other constructor starts to execute
CircleWithStaticVariable class (not instance) with static data numOfObjects 1 this constructor frame newRadius 5.0 CircleWithStaticVariables object radius 1.0 main’s frame args circle1 CircleWithStaticVariables object radius circle2 Frame Stack Heap
16
CircleWithStaticVariable class (not instance) with static data
numOfObjects 1 Instance variable is set constructor frame newRadius 5.0 this CircleWithStaticVariables object radius 1.0 main’s frame args circle1 CircleWithStaticVariables object radius 5.0 circle2 Frame Stack Heap
17
CircleWithStaticVariable class (not instance) with static data
numOfObjects 2 Static variable is set constructor frame newRadius 5.0 this CircleWithStaticVariables object radius 1.0 main’s frame args circle1 CircleWithStaticVariables object radius 5.0 circle2 Frame Stack Heap
18
Constructor terminates and address is assigned into reference
CircleWithStaticVariable class (not instance) with static data numOfObjects 2 CircleWithStaticVariables object radius 1.0 main’s frame args circle1 CircleWithStaticVariables object radius 5.0 circle2 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 pp
21
Frame Stack Heap main’s args frame circle Array
Create the local variable…a reference to an array of circle objects. NOT an array. NOT a circle object. main’s frame args circle Array Frame Stack Heap
22
Frame Stack Heap create circle Circle Array Array’s frame i main’s
Method call. Frame goes on stack. create Circle Array’s frame circle Array i main’s frame args circle Array Frame Stack Heap
23
Frame Stack Heap create Circle Array’s frame circle Array i main’s
Instantiate the array (not the Circle objects). create Circle Array’s frame circle Array i main’s frame args circle Array 1 2 3 9 Frame Stack Heap
24
Frame Stack Heap constructor frame newRadius 52.3 this create Circle
Instantiate a Circle object. Constructor is invoked. create Circle Array’s frame Circle object radius 52.3 circle Array i main’s frame args circle Array 1 2 9 Frame Stack Heap
25
Frame Stack Heap create Circle Array’s frame circle Array i main’s
Constructor terminates. Return value of new is assigned into array element. create Circle Array’s frame Circle Object radius 52.3 circle Array i main’s frame args circle Array 1 2 9 Frame Stack Heap
26
Frame Stack Heap this constructor frame newRadius 28.4 create Circle
Instantiate another Circle object. Constructor is invoked. create Circle Array’s frame Circle Object radius 52.3 Circle Object radius 28.4 circle Array i 1 main’s frame args circle Array 1 2 9 Frame Stack Heap
27
Frame Stack Heap create Circle Array’s frame circle Array i 1 main’s
Constructor terminates. Return value of new is assigned into next array element. create Circle Array’s frame Circle Object radius 52.3 Circle Object radius 28.4 circle Array i 1 main’s frame args circle Array 1 2 9 Frame Stack Heap
28
Frame Stack Heap main’s frame args circle Array 1 2 9
Eventually, array is filled. Array reference gets returned. Return value is assigned into main’s local variable. Circle Object radius 52.3 Circle Object radius 28.4 Circle Object radius 93.7 Circle Object radius 11.9 main’s frame args circle Array 1 2 9 Frame Stack Heap
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.