MATLAB Object-Oriented Programming

(Joyce) #1

Static Data


In this section...
“What Is Static Data” on page 4-2
“Static Variable” on page 4-2
“Static Data Object” on page 4-3
“Constant Data” on page 4-5

What Is Static Data


Static data refers to data that all objects of the class share and that you can modify after
creation.

Use static data to define counters used by class instances or other data that is shared
among all objects of a class. Unlike instance data, static data does not vary from one
object to another. MATLAB provides several ways to define static data, depending on your
requirements.

Static Variable


Classes can use a persistent variable to store static data. Define a static method or local
function in which you create a persistent variable. The method or function provides
access to this variable. Use this technique when you want to store one or two variables.

Saving an object of the class defining the persistent variable does not save the static data
associated with the class. To save your static data in an object, or define more extensive
data, use the static data object technique “Static Data Object” on page 4-3

Implementation

The StoreData class defines a static method that declares a persistent variable Var. The
setgetVar method provides set and get access to the data in the persistent variable.
Because the setgetVar method has public access, you can set and get the data stored in
the persistent variable globally. Control the scope of access by setting the method Access
attribute.

classdef StoreData
methods (Static)

4 Static Data

Free download pdf