Science, Tech, Math › Computer Science Understanding Delphi Class Methods Share Flipboard Email Print Getty Images/Emilija Manevska Computer Science Delphi Programming Delphi Tutorials Advanced PHP Programming Perl Python Java Programming Javascript Programming C & C++ Programming Ruby Programming Visual Basic View More By Zarko Gajic Zarko Gajic Twitter Computer Science Expert MSCS, Computer Science, University of Osijek Zarko Gajic is experienced in SQL and has working knowledge of DB systems such as MS SQL Server, Firebird, Interbase, and Oracle. He is also proficient in XML, DHTML, and JavaScript. Learn about our Editorial Process Updated on February 14, 2019 In Delphi, a method is a procedure or function that performs an operation on an object. A class method is a method that operates on a class reference instead of an object reference. If you read between the lines, you will find that class methods are accessible even when you haven't created an instance of the class (the object). Class Methods vs. Object Methods Every time you create a Delphi component dynamically, you use a class method: the Constructor. The Create constructor is a class method, as opposed to virtually all other methods you'll encounter in Delphi programming, which are object methods. A class method is a method of the class, and appropriately enough, an object method is a method that can be called by an instance of the class. This is best illustrated by an example, with classes and objects highlighted in red for clarity: myCheckbox := TCheckbox.Create(nil) ; Here, the call to Create is preceded by the class name and a period ("TCheckbox."). It's a method of the class, commonly known as a constructor. This is the mechanism by which instances of a class are created. The result is an instance of the TCheckbox class. These instances are called objects. Contrast the previous line of code with the following: myCheckbox.Repaint; Here, the Repaint method of the TCheckbox object (inherited from TWinControl) is called. The call to Repaint is preceded by the object variable and a period ("myCheckbox."). Class methods can be called without an instance of the class (e.g., "TCheckbox.Create"). Class methods can also be called directly from an object (e.g., "myCheckbox.ClassName"). However object methods can only be called by an instance of a class (e.g., "myCheckbox.Repaint"). Behind the scenes, the Create constructor is allocating memory for the object (and performing any additional initialization as specified by TCheckbox or its ancestors). Experimenting With Your Own Class Methods Think of AboutBox (a custom "About This Application" form). The following code uses something like:procedure TfrMain.mnuInfoClick(Sender: TObject) ;beginAboutBox:=TAboutBox.Create(nil) ;tryAboutBox.ShowModal;finallyAboutBox.Release;end;end;This, of course, is a very nice way to do the job, but just to make the code easier to read (and to manage), it would be much more efficient to change it to:procedure TfrMain.mnuInfoClick(Sender: TObject) ;beginTAboutBox.ShowYourself;end;The above line calls the "ShowYourself" class method of the TAboutBox class. The "ShowYourself" must be marked with the keyword "class":class procedure TAboutBox.ShowYourself;beginAboutBox:= TAboutBox.Create(nil) ;tryAboutBox.ShowModal;finallyAboutBox.Release;end;end; Things to Keep in Mind The definition of a class method must include the reserved word class before the procedure or function keyword that starts the definition.AboutBox form is not auto-created (Project-Options).Put AboutBox unit to the uses clause of the main form.Don't forget to declare the procedure in the interface (public) part of the AboutBox unit. Cite this Article Format mla apa chicago Your Citation Gajic, Zarko. "Understanding Delphi Class Methods." ThoughtCo, Aug. 27, 2020, thoughtco.com/understanding-class-methods-1058182. Gajic, Zarko. (2020, August 27). Understanding Delphi Class Methods. Retrieved from https://www.thoughtco.com/understanding-class-methods-1058182 Gajic, Zarko. "Understanding Delphi Class Methods." ThoughtCo. https://www.thoughtco.com/understanding-class-methods-1058182 (accessed May 30, 2023). copy citation Featured Video