Anatomy of an Object Pascal file
Object Pascal extends Pascal’s structured foundation with classes, interfaces, exceptions, and other object-oriented features; Delphi and Free Pascal are its best-known modern implementations.
File extensions: .pas, .pp
Every part of the example below is labelled and explained. This page is one of 55 annotated tours on AnatomyOf, a free, open-source project by LunarWerx Studios.
What is inside an Object Pascal file
Program heading
Names an executable program with program Name;.
A Pascal executable commonly starts with a program heading followed by an identifier and semicolon. The name documents the compilation unit and may be used by tooling, but it is not a function that the runtime calls. A unit uses unit Name; instead and exposes an interface plus an implementation. Libraries can use library Name;. The final punctuation also changes by role: a complete program or unit finishes with end.. The period is part of the syntax, not prose.
Compiler directive
A {$...} directive selects a language mode or changes compilation behavior.
Compiler directives are instructions consumed during compilation rather than statements executed at runtime. {$mode objfpc} enables Free Pascal’s Object Pascal dialect, while {$H+} selects long AnsiString values for the unqualified string type. Directives can also enable checks, define conditional symbols, or include files. Their exact set is implementation-specific, so portable code uses them sparingly and keeps dialect assumptions explicit near the top of the file.
Uses clause
Imports units so their exported declarations are available.
uses SysUtils; makes the public interface of the SysUtils unit visible to this program. A comma-separated list can import multiple units, and the compiler follows those dependencies before compiling or linking the current unit. Unlike a textual include, a unit has a compiled interface and its own initialization/finalization lifecycle. Name conflicts can be resolved with a qualified name such as SysUtils.Format.
Class definition
Declares an object type with fields, constructors, and methods.
TGreeter = class ... end; defines a reference-type class. By convention, Pascal type names begin with T; the convention is widespread but not enforced by the compiler. Class instances live on the heap and variables hold references to them. In Free Pascal and Delphi-style code, creating an instance with a constructor is normally paired with an explicit Free call, often protected by try...finally.
Visibility section
private and public control which class members callers may access.
Visibility labels divide a class declaration into sections. private hides implementation details such as FName, while public exposes the constructor and Message method to callers. Object Pascal also supports protected, published, and, depending on mode and placement, stricter unit-aware variants. The labels apply to every following member until another visibility label appears.
Method declaration
Declares a constructor, procedure, or value-returning function on a class.
The class body publishes each method’s signature. A procedure performs work without returning a value; a function declares a result type after its parameter list; and a constructor creates and initializes an instance. The declaration ends with a semicolon because its executable body appears later. The qualified implementation name, such as TGreeter.Message, connects that later body back to this declaration.
Method implementation
Provides a class method’s executable begin ... end body.
function TGreeter.Message: string; qualifies the method with its owning class. Local declarations, if any, would appear between the signature and begin; executable statements follow inside the block. A function returns through the special Result variable in modern Object Pascal. Assigning Result := ... is clearer and safer than the older syntax that assigns to the function’s own name.
Variable declaration
Declares a typed name in a var section before executable statements.
var Greeter: TGreeter; declares a variable whose type is known at compile time. Pascal groups declarations before the begin that opens the surrounding executable block rather than allowing every dialect to introduce variables freely among statements. A declaration does not construct a class instance. The reference receives an object only after the constructor call is assigned to it.
Assignment (`:=`)
Stores a value with :=; plain = tests equality or appears in declarations.
Pascal deliberately separates assignment (:=) from equality (=). FName := AName changes a field, while FName = AName is a Boolean expression that compares two values. The same assignment operator stores the object reference returned by TGreeter.Create. Keeping mutation visually distinct from comparison is one of Pascal’s readability-first design choices.
Main program block
The final begin ... end. block is the executable entry point.
After all declarations, the outermost begin starts the statements executed when the program launches. Nested blocks end with end;, but the outer program closes with end. to mark the end of the source unit. Semicolons separate statements; they are not unconditional line terminators. In particular, placing one immediately before an else can accidentally end the preceding if statement.
Deterministic cleanup
try...finally guarantees cleanup whether the protected code succeeds or raises.
Statements in the finally block run when control leaves the try block normally or because of an exception. That makes it the conventional home for Greeter.Free, file closing, lock release, and similar cleanup. Calling Free is safe when an object reference is nil; it invokes the destructor only for a real instance. try...except is the sibling construct for handling an exception rather than merely guaranteeing cleanup.
Official Object Pascal site · All languages on AnatomyOf
The interactive tour needs JavaScript. Enable it to hover a callout and trace it into the code.