1. Computing & Technology

Discuss in my forum

Class Method

By , About.com Guide

Definition: A class method is a method that operates on classes instead of objects.

The definition of a class method must begin with the reserved word class.

The most common used class method in Delphi language is the "Create" constructor.

In the defining declaration of a class method, the identifier Self represents the class where the method is called (which could be a descendant of the class in which it is defined). If the method is called in the class TCar, then Self is of the type class of TCar. Thus you cannot use Self to access fields, properties, and normal (object) methods, but you can use it to call constructors and other class methods.

A class method can be called through a class reference or an object reference. When it is called through an object reference, the class of the object becomes the value of Self.

Examples:
 type
   TCar = Class
     //"object" method
     procedure Drive;
     //class method
     class procedure Stop;
   end;
 
   ...
 
   class procedure Stop;
   begin
    ...
   end; 

©2012 About.com. All rights reserved.

A part of The New York Times Company.