Documentation

handle class

Abstract class for deriving handle classes

Description

classdef MyHandleClass < handle makes MyHandleClass a subclass of the handle class.

The handle class is the superclass for all classes that follow handle semantics. A handle is a reference to an object. If you copy an object's handle variable, MATLAB® copies only the handle. Both the original and copy refer to the same object. For example, if a function modifies a handle object passed as an input argument, the modification affects the original input object.

In contrast, nonhandle objects (that is, value objects) are not references. Functions must return modified value objects to change the object outside of the function's workspace.

See Modifying Objects for information on passing objects to functions.

If you want to create a class that defines events, you must derive that class from the handle class.

The handle class is an abstract class, so you cannot create an instance of this class directly. You use the handle class to derive other classes, which can be concrete classes whose instances are handle objects. See Handle Classes for information on using handle classes.

Methods

addlistenerCreate event listener
deleteDelete handle object
findobjFind handle objects
findpropFind meta.property object
isvalidDetermine valid handles
notifyNotify listeners that event is occurring
relationaloperatorsDetermine equality or sort handle objects

Events

ObjectBeingDestroyedTriggered when the handle object is about to be destroyed, but before calling the delete method. Listener callbacks execute before MATLAB destroys the handle object.

Destroying Handle Objects

MATLAB destroys handle objects when there are no references to the object. You can explicitly remove a handle object by calling its delete method. The handle class enables you to control what happens when handle objects are destroyed, either implicitly when no references exist or explicitly when you delete the object.

Define delete Method in Subclass

Subclasses of handle can define their own delete method. MATLAB calls this method when deleting an object of the subclass, enabling your subclass to perform any specific actions necessary before deleting an object.

For more information, see Handle Class Destructor.

Create Listener for ObjectBeingDestroyed Event

Any code can respond to the pending deletion of a handle object by defining a listener for that object's ObjectBeingDestroyed event. MATLAB triggers this event before calling the object's delete method.

For more information on using events and listeners, see Events and Listeners — Syntax and Techniques.

Attributes

Abstracttrue
ConstructOnLoadtrue
HandleCompatibletrue

To learn about attributes of classes, see Class Attributes in the MATLAB Object-Oriented Programming documentation.

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects in the MATLAB documentation.

Examples

Derive Class from handle

Derive MyClass from handle:

classdef MyClass < handle
   properties
      Prop1
   end
   methods
      function obj = MyClass(arg)
         obj.Prop1 = arg;
      end
   end
end
Was this topic helpful?