ProgramGuru

Classes and shared references

Classes and shared references

A shared class register may need one identity observed by several parts of a program. A class instance is a reference type. Assigning its reference to another name makes both names refer to the same object rather than creating an independent record.

What you’ll learn

  • Trace two references to the same class instance.
  • Distinguish identity, rebinding and property mutation.

Classes and shared references in the example

  1. One instance

    A shared class register may need one identity observed by several parts of a program.

    One instance
  2. Two references

    Classes can store properties, define methods and have explicit initializers.

    Two references
  3. One shared count

    Both references point at one register, so changing count through second is visible through first.

    One shared count
Both references point at one register, so changing count through second is visible through first. Both bindings are let, but the class’s var property can still change. Final prevents subclassing; it does not make the instance immutable.

How classes and shared references works

Classes can store properties, define methods and have explicit initializers. Identity comparison with === asks whether references point to the same instance; equality with == is a separate value-comparison contract when the type provides it. Choose a reference type when shared identity is intentional.

Work through classes and shared references

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

final class Register {
  var count: Int
  init(count: Int) { self.count = count }
}
let first = Register(count: 2)
let second = first
second.count += 1
print(first.count)
print(first === second)

Expected output

3
true

Follow the execution

Both references point at one register, so changing count through second is visible through first. Both bindings are let, but the class’s var property can still change. Final prevents subclassing; it does not make the instance immutable.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Classes and shared references

Your task: What would you need for a separate register with count two?

Swift · complete program

final class Register {
  var count: Int
  init(count: Int) { self.count = count }
}
let first = Register(count: 2)
let second = first
second.count += 1
print(first.count)
print(first === second)

Worked answer

Create another Register(count: 2) instance. Merely assigning first to a new name shares its existing identity.

Choose shared identity intentionally

Prefer a struct when independent value behaviour fits the domain. Shared references require attention to mutation, ownership and concurrent access. ARC manages lifetime but does not prevent every logical mistake involving shared state.

Check your decision

What would you need for a separate register with count two?
Choose one answer. You can try again.

Answer and explanation

Create another Register(count: 2) instance.

Create another Register(count: 2) instance. Merely assigning first to a new name shares its existing identity.

Check the practical consequence

A let reference points at a class with a var count. Which operation is allowed here?
Choose one answer. You can try again.

Answer and explanation

Changing the count property without reassigning the reference.

Prefer a struct when independent value behaviour fits the domain. Shared references require attention to mutation, ownership and concurrent access. ARC manages lifetime but does not prevent every logical mistake involving shared state.

Choose shared identity intentionally

Create another Register(count: 2) instance. Merely assigning first to a new name shares its existing identity. Prefer a struct when independent value behaviour fits the domain.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Stored properties, computed properties and observers →