Menu
Coddy logo textTech

Setter Methods

Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 14 of 70.

Getter methods let you read internal data through a controlled access point. Setter methods (also called mutator methods) do the opposite—they provide a controlled way to modify internal state.

Instead of directly assigning car.speed = 50, you call car:setSpeed(50):

local Car = {}
Car.__index = Car

function Car:new()
    local instance = {}
    setmetatable(instance, self)
    instance.speed = 0
    return instance
end

function Car:setSpeed(newSpeed)
    self.speed = newSpeed
end

function Car:getSpeed()
    return self.speed
end

At first glance, this seems like extra work for the same result. But setters become powerful when you need to add rules later. Imagine you want to ensure speed never goes negative or exceeds a maximum:

function Car:setSpeed(newSpeed)
    if newSpeed < 0 then
        self.speed = 0
    elseif newSpeed > 200 then
        self.speed = 200
    else
        self.speed = newSpeed
    end
end

Now every speed change goes through your validation logic. Any code using :setSpeed() automatically benefits from these checks without modification. This is the real value of setters—they create a single point where you control how data changes, making your objects safer and easier to maintain.

challenge icon

Challenge

Easy

Let's build a Thermostat class that uses setter methods to control temperature in a smart way. Instead of allowing any temperature value, your thermostat will enforce reasonable limits—keeping the temperature within a comfortable range.

You'll organize your code across two files:

  • Thermostat.lua: Define your Thermostat class with the standard prototype pattern. Each thermostat should track its current temperature. Include:
    • A :new() constructor that initializes the temperature to 20 (a comfortable default)
    • A :getTemperature() getter that returns the current temperature
    • A :setTemperature(newTemp) setter that updates the temperature, but keeps it within bounds: minimum 10 and maximum 30
  • main.lua: Require your Thermostat module and create a thermostat instance. Use the setter to attempt different temperature changes and observe how the limits are enforced.

Your :setTemperature() method should enforce these rules:

  • If the new temperature is below 10, set it to 10
  • If the new temperature is above 30, set it to 30
  • Otherwise, set it to the requested value

You will receive two inputs:

  1. First temperature to set
  2. Second temperature to set

In your main file, create a thermostat, then:

  1. Print the initial temperature
  2. Set the temperature to the first input value and print the result
  3. Set the temperature to the second input value and print the result

Print each temperature on its own line (just the number).

For example, if the inputs are 25 and 50, the output should be:

20
25
30

Notice how the second attempt to set 50 results in 30—the setter enforces the maximum limit automatically!

Cheat sheet

Setter methods provide a controlled way to modify internal state, allowing you to add validation rules and constraints.

Basic setter method syntax:

function Car:setSpeed(newSpeed)
    self.speed = newSpeed
end

Setters with validation enforce rules when data changes:

function Car:setSpeed(newSpeed)
    if newSpeed < 0 then
        self.speed = 0
    elseif newSpeed > 200 then
        self.speed = 200
    else
        self.speed = newSpeed
    end
end

Combining getters and setters creates controlled access to object properties:

local Car = {}
Car.__index = Car

function Car:new()
    local instance = {}
    setmetatable(instance, self)
    instance.speed = 0
    return instance
end

function Car:setSpeed(newSpeed)
    if newSpeed < 0 then
        self.speed = 0
    elseif newSpeed > 200 then
        self.speed = 200
    else
        self.speed = newSpeed
    end
end

function Car:getSpeed()
    return self.speed
end

Setters create a single point of control for data modification, making objects safer and easier to maintain. Any code using the setter automatically benefits from validation logic without modification.

Try it yourself

-- Require the Thermostat module
local Thermostat = require('Thermostat')

-- Read input
local temp1 = tonumber(io.read())
local temp2 = tonumber(io.read())

-- TODO: Create a thermostat instance

-- TODO: Print the initial temperature

-- TODO: Set the temperature to temp1 and print the result

-- TODO: Set the temperature to temp2 and print the result
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming