Skip to content

Instances

Instances are self contained objects that contain the methods and properties that were originally set up in the class or subclass template they are instatiated from.

Instances have their own state once created. If the class supports it, you can pass arguments when you create an instance, allowing you to customize the state of each instance.

Multiple instances can be created from a single class or subclass. Instances can be passed to other classes and subclasses as well.

Discussion

Conventions

An instance name is generally represented as a lowercase string. For example, "person" is a good instance identifier.

Instance Creation

Instances are created using the new method on the class you want to create an instance of. If a class supports arguments, you can also pass those along with the new method.

See the Classes and Subclasses sections for details on creating class templates.

Calling Methods

Instance methods are always called using the Lua colon (:) syntax, for example:

--using colon syntax
local name = person:getName()

--THIS WILL NOT WORK
--using dot syntax
local name = person.getName()

Get/Set Properties

To get or set any properties of the instance, you use the standard dot (.) syntax, for example:

--get
local name = person.name

--set
person.name = "Billy"

Instance Example

local Person = require("classes.Person")

local p1 = Person:new("Tammy")
local p2 = Person:new("Jimmy")

print(p1:getName()) --> Tammy
print(p2:getName()) --> Jimmy

See the class example here to get an idea of what the class looks like that produces the instances in the example above.