My understanding is that the static
keyword was introduced for compatibility with .NET (along with strict
)
class TExample
class procedure First;
class procedure Second; static;
The differences between procedures First
and Second
are :-
First
can be overridden in a descendant classFirst
passes an implicit self parameter referencing the TExample
class.Class procedure Second
cannot be overridden and passes no parameters and is thus .NET compatible. So is there any point in using the static
keyword in native-only code now that there is a divergence between Delphi & Prism syntax?
The static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given class. The users can apply static keywords with variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class.
It means that only one instance of a static field exists even if you create a million instances of the class or you don't create any. It will be shared by all instances. Since static methods also do not belong to a specific instance, they can't refer to instance members.
The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc. The static variable gets memory only once in the class area at the time of class loading.
One such frequently used keyword in Java is the “Static” keyword. The most important reason why static keywords are heavily used in Java is to efficiently manage memory. Generally, if you want to access variables or methods inside a class, you first need to create an instance or object of that class.
Static class methods have no hidden class reference argument. Because of this, they are assignment compatible with plain old function pointers, and can therefore be used for interaction with the Windows API and other C APIs. Example:
type
TForm = class
private
class function NonStaticWndProc (wnd: HWND; Message: Cardinal;
wParam: WPARAM; lParam: LPARAM): LRESULT;
class function StaticWndProc (wnd: HWND; Message: Cardinal;
wParam: WPARAM; lParam: LPARAM): LRESULT; static;
procedure RegisterClass;
end;
procedure TForm.RegisterClass;
type
TWndProc = function (wnd: HWND; Message: Cardinal;
wParam: WPARAM; lParam: LPARAM): LRESULT;
var
WP: TWndProc;
WindowClass: WNDCLASS;
begin
//WP := NonStaticWndProc; // doesn't work
WP := StaticWndProc; // works
// ...
TWndProc (WindowClass.lpfnWndProc) := WP;
Windows.RegisterClass (WindowClass);
end;
(Of course, you could have used a global function instead, but other than global functions, static class functions have a clear association with a class.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With