11
votes

I have a question about use of global variables and variables of class using class var.

Declaring variables in class with class var:

unit Unit1;

interface

type
  TClass = class
  public
    class var ObjectList: TObjectList
  end;

implementation
end.

Declaring global variables:

unit Unit1;

interface

var
  ObjectList: TObjectList

implementation
end.

How does the compiler allocate memory for these two variables?

1
Worth to mention what in both cases ObjectList pointer will reside in DATA segment.Free Consulting

1 Answers

8
votes

These variables are implemented in exactly the same way. The class var is implemented as a global variable. That is there is a single instance of the variable in the module, allocated statically.

The only difference is that the class var is in a different scope, and you can use visibility protection specifiers like private to limit access to the variable.