Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Delphi's static keyword have any point in native-only code?

Tags:

oop

class

delphi

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 :-

  1. First can be overridden in a descendant class
  2. First 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?

like image 744
MikeJ-UK Avatar asked Jul 17 '09 10:07

MikeJ-UK


People also ask

Where can static keyword used?

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.

Why static is used in Java stack overflow?

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.

What are valid statements for the static keyword in Java select all that apply?

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.

Why static keyword is used in Java?

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.


1 Answers

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.)

like image 173
Moritz Beutel Avatar answered Sep 19 '22 20:09

Moritz Beutel