(Yes, I know this thread is outdated, but I thought I would post it for posterity.)
It was pointed out that class functions and class procedures implement static methods. But I will add that the next notable behavior of the static class (against the Delphi class) is that the static class cannot be created.
Delphi classes receive an open default constructor (without parameters) if you do not specify it, so any instance can be created. If you declare one or more constructors explicitly, this constructor is not provided.
You can remove all constructors by declaring the constructor in a private or protected section of your class. This removes your constructor from the scope of the consumer. Now there is only one constructor, it is not displayed and the class cannot be created.
Example:
type TMyStaticClass = class(TObject) private // Hide the default constructor, suppressing hint H2219. {$HINTS OFF} constructor Create; {$HINTS ON} public class procedure Method1;
If you have one of the newer versions of Delphi, you can also consider sealing the class to be a little more correct. The COULD descendant class is created if your constructor is protected, not private.
Edit
Here's an Ken-inspired alternative for newer versions of Delphi (which support private classes) that don't require compiler hints. This is not an ideal solution, but here it is.
unit Unit1; interface type TMyStaticClass = class sealed (TObject) protected constructor Create; public class procedure Method1; // A static method end; implementation constructor TMyStaticClass.Create; begin // Do nothing. This cannot be called. end; class procedure TMyStaticClass.Method1(); begin // Do something here. end; end.
Phil gilmore
source share