Pascal objects and classes
last modified November 19, 2026 Free Pascal 3.2.2
A class is a blueprint for an object. It groups fields, which hold the data, with methods, which operate on that data. An object is one instance of the class, created on the heap, and a variable of a class type holds a reference to that instance. This article builds a class one piece at a time: a field, a method, object references, a constructor, a destructor, visibility, properties, and an object that owns another object.
An object with a field
A class is declared with the keyword class and ends with
end and a semicolon. The first example has one field and no
methods. TPerson.Create is inherited from TObject, the
root of every class hierarchy. The constructor call allocates a zero-filled
instance and returns a reference. Because this class does not declare a
constructor, the inherited one takes no arguments.
program person1;
{$mode objfpc}{$H+}
type
TPerson = class
Name: string;
end;
var
P: TPerson;
begin
P := TPerson.Create;
P.Name := 'Alice';
WriteLn(P.Name);
P.Free;
end.
The variable P is not the object; it is a pointer to the object on
the heap. P.Name := 'Alice' stores a string inside that object, and
P.Name reads it back. P.Free calls the destructor and
releases the memory. The field was empty before the assignment, because a new
object starts with zeroed bytes.
$ fpc -gl person1.pas $ ./person1 Alice
Adding a method
A method is declared inside the class and implemented outside it. The
implementation repeats the class name before the method name, as in
TPerson.Greet, and can read the fields of the object directly.
Inside the method, Name means Self.Name, where
Self is the object the method was called on.
program person2;
{$mode objfpc}{$H+}
type
TPerson = class
Name: string;
procedure Greet;
end;
procedure TPerson.Greet;
begin
WriteLn('Hello, ', Name);
end;
var
P: TPerson;
begin
P := TPerson.Create;
P.Name := 'Alice';
P.Greet;
P.Free;
end.
The call P.Greet passes P as the hidden
Self parameter. The method prints the field of that particular
object, so a second TPerson with a different name would print a
different line. A method can have parameters and a result just like an ordinary
procedure or function; the class only changes where it is declared and how it is
called.
$ fpc -gl person2.pas $ ./person2 Hello, Alice
References, not values
Assigning one class variable to another copies the reference, not the object. Both variables then point at the same instance, and a change made through one name is visible through the other. This is the most important difference between a class and a record, which is copied field by field.
program refs;
{$mode objfpc}{$H+}
type
TPerson = class
Name: string;
end;
var
A, B: TPerson;
begin
A := TPerson.Create;
A.Name := 'Alice';
B := A;
B.Name := 'Bob';
WriteLn('A: ', A.Name);
WriteLn('B: ', B.Name);
WriteLn('same object: ', A = B);
A.Free;
end.
B := A makes B an alias for the object created in
A, so writing B.Name changes what A.Name
returns. The comparison A = B compares the addresses and is true.
The object is freed once, through A; calling Free
through B as well would be a double free. After the free, both
variables dangle, which is why FreeAndNil from SysUtils
sets the variable to nil at the same time.
$ fpc -gl refs.pas $ ./refs A: Bob B: Bob same object: TRUE
A constructor
A constructor is a special method that initializes a new object. It is declared
with the keyword constructor and is called through the class name,
as in TPerson.Create('Alice'). The constructor runs after the
memory has been allocated and before the reference is returned to the caller.
program ctor;
{$mode objfpc}{$H+}
type
TPerson = class
private
FName: string;
public
constructor Create(const AName: string);
procedure Greet;
end;
constructor TPerson.Create(const AName: string);
begin
FName := AName;
end;
procedure TPerson.Greet;
begin
WriteLn('Hello, ', FName);
end;
var
P: TPerson;
begin
P := TPerson.Create('Alice');
P.Greet;
P.Free;
end.
The constructor stores its argument in the private field, so the object is
usable as soon as Create returns. A class can declare more than one constructor,
either with different names or as overloads with different parameter lists; the
compiler chooses one from the arguments at the call. The name
Create is a convention inherited from TObject, not a
language rule, but every Pascal programmer expects it.
$ fpc -gl ctor.pas $ ./ctor Hello, Alice
A destructor
A destructor releases what the object owns. It is declared with
destructor, and the override keyword is required
because TObject already declares a virtual destructor named
Destroy. Free is a method of TObject that
calls Destroy; it also checks for nil, so freeing a
nil reference does nothing.
program dtor;
{$mode objfpc}{$H+}
type
TPerson = class
private
FName: string;
public
constructor Create(const AName: string);
destructor Destroy; override;
property Name: string read FName;
end;
constructor TPerson.Create(const AName: string);
begin
FName := AName;
end;
destructor TPerson.Destroy;
begin
WriteLn('goodbye ', FName);
inherited Destroy;
end;
var
P: TPerson;
begin
P := TPerson.Create('Alice');
WriteLn('hello ', P.Name);
P.Free;
end.
P.Free runs TPerson.Destroy, which prints the name and
then calls inherited Destroy. The inherited call runs the ancestor
destructor, which releases whatever the ancestor owns; skipping it skips that
cleanup. Free releases the object's memory after the destructor
chain returns. The property Name is read-only here because it has
no write clause, so the destructor can read the field but callers
cannot rename the object after construction.
$ fpc -gl dtor.pas $ ./dtor hello Alice goodbye Alice
Private fields and properties
A field placed in a private section is hidden from the public
interface of the class. A property gives that field a public name and controls
whether it can be read, written, or both. A property does not allocate
storage; it names a field or a pair of methods that the compiler uses on every
read or write.
program prop1;
{$mode objfpc}{$H+}
type
TPerson = class
private
FName: string;
public
constructor Create(const AName: string);
property Name: string read FName write FName;
end;
constructor TPerson.Create(const AName: string);
begin
FName := AName;
end;
var
P: TPerson;
begin
P := TPerson.Create('Alice');
WriteLn(P.Name);
P.Name := 'Bob';
WriteLn(P.Name);
P.Free;
end.
property Name: string read FName write FName makes
P.Name look like a field to the caller, while the storage stays in
the private FName. In ObjFPC a private member is
visible to the whole unit, not only to the class; strict private
limits it to the class itself. The property is the part that other units see,
and it can later gain validation or a computed value without changing the call
syntax.
$ fpc -gl prop1.pas $ ./prop1 Alice Bob
A property with a getter
A property can compute its value instead of reading a field. The getter is a
method with no parameters whose result type matches the property type. A
read-only property has only a read clause, so assigning to it is a
compile-time error.
program prop2;
{$mode objfpc}{$H+}
type
TPerson = class
private
FName: string;
function GetInitial: Char;
public
constructor Create(const AName: string);
property Name: string read FName write FName;
property Initial: Char read GetInitial;
end;
constructor TPerson.Create(const AName: string);
begin
FName := AName;
end;
function TPerson.GetInitial: Char;
begin
if FName = '' then
Result := '?'
else
Result := FName[1];
end;
var
P: TPerson;
begin
P := TPerson.Create('Alice');
WriteLn(P.Name, ' starts with ', P.Initial);
P.Free;
end.
Initial is not stored anywhere: each access calls
GetInitial, which returns the first character of the name or a
question mark for an empty name. A setter would work the same way, with a
parameter and a write clause. Computed properties keep derived
values consistent, because there is no second copy that can fall out of date.
$ fpc -gl prop2.pas $ ./prop2 Alice starts with A
An object that owns another object
A class can create other objects and keep them in fields. The owner is
responsible for freeing them: the constructor creates them, and the destructor
releases them before calling inherited Destroy. This is
composition, the "has a" relationship, and it is usually a better fit than
inheritance when one object simply uses another.
program notebook;
{$mode objfpc}{$H+}
uses Classes;
type
TNotebook = class
private
FLines: TStringList;
public
constructor Create;
destructor Destroy; override;
procedure Add(const S: string);
function Count: Integer;
end;
constructor TNotebook.Create;
begin
FLines := TStringList.Create;
end;
destructor TNotebook.Destroy;
begin
FLines.Free;
inherited Destroy;
end;
procedure TNotebook.Add(const S: string);
begin
FLines.Add(S);
end;
function TNotebook.Count: Integer;
begin
Result := FLines.Count;
end;
var
N: TNotebook;
begin
N := TNotebook.Create;
N.Add('first note');
N.Add('second note');
WriteLn('notes: ', N.Count);
N.Free;
end.
TNotebook.Create creates a TStringList and stores it
in FLines. Every Add forwards to that list, and
Count reports its size. TNotebook.Destroy frees the
list first, then calls the inherited destructor. Freeing owned objects first
keeps the object fully valid while the ancestor destructor runs;
inherited Destroy is always the last statement.
$ fpc -gl notebook.pas $ ./notebook notes: 2
TObject and the class hierarchy
Every class implicitly descends from TObject, even when the
declaration does not name an ancestor. TObject provides
Create, Free, ClassName, and
InheritsFrom, among other methods. A variable of type
TObject can hold any object, which is how
TObjectList and similar containers store heterogeneous values.
program rootobj;
{$mode objfpc}{$H+}
type
TPerson = class
end;
var
P: TPerson;
begin
P := TPerson.Create;
WriteLn('class: ', P.ClassName);
WriteLn('is TObject: ', P is TObject);
P.Free;
P := nil;
P.Free;
WriteLn('free on nil is safe');
end.
ClassName returns the name of the actual class,
TPerson, and P is TObject is true for every object.
InheritsFrom answers the same question with an explicit class
argument, which is useful when the ancestor is not known at compile time.
After P := nil, the second P.Free is safe because
Free checks for nil. FreeAndNil(P) combines
the two steps; it needs SysUtils in the uses clause.
$ fpc -gl rootobj.pas $ ./rootobj class: TPerson is TObject: TRUE free on nil is safe
Next steps
This article creates and frees objects explicitly. The
error handling and exceptions article shows the
try..finally pattern that frees an object even when the code
between creation and release raises. The
inheritance article adds
virtual, override, and abstract methods,
and the interfaces article shows how a class
can promise several independent contracts.
Source
In this article we declared a class, added a method, followed a reference through two variables, wrote a constructor and a destructor, hid a field behind a property, computed a property with a getter, and built an object that owns another object. The series continues with error handling and exceptions in Pascal.
Author
List all Pascal tutorials.