6.3.1 Normal fields/variables

There are two ways to declare a normal field. The first one is the classical way, similar to a definition in an object:

{$mode objfpc}
type
  cl=class
    l : longint;
  end;
var
  cl1,cl2 : cl;
begin
  cl1:=cl.create;
  cl2:=cl.create;
  cl1.l:=2;
  writeln(cl1.l);
  writeln(cl2.l);
end.

will be the following

2
0

The example demonstrates that values of fields are initialized with zero (or the equivalent of zero for non ordinal types: empty string, empty array and so on).

The second way to declare a field (only available in more recent versions of Free Pascal) is using a var block:

{$mode objfpc}
type
  cl=class
  var
    l : longint;
  end;

This definition is completely equivalent to the previous definition.

Remark As of version 3.0 of the compiler, the compiler can re-order the fields in memory if this leads to better alignment and smaller instances. That means that in an instance, the fields do not necessarily appear in the same order as in the declaration. RTTI generated for a class will reflect this change.