Browse Questions
- Both static and class define type-level properties and methods, but static members cannot be overridden by subclasses (final), whereas class members allow dynamic dispatch and can be overridden.
- Use Secure Message when two parties with separate key pairs exchange data, and Secure Cell when a single party encrypts data for itself.
- Polymorphism lets code work with values of different underlying types through a single shared interface.
- A lazy property is only computed the first time it is accessed.
Answer:
Both static and class define type-level properties and methods, but static members cannot be overridden by subclasses (final), whereas class members allow dynamic dispatch and can be overridden.
| Feature | static | class |
|---|---|---|
| Can be overridden? | No (implicitly final) | Yes (supports polymorphism) |
| Supported types | class, struct, enum, actor, protocol | class only (and class-bound protocols) |
| Stored properties | Yes (static let / static var) | No (computed properties only) |
| Dispatch mechanism | Static / direct dispatch (fast) | Dynamic / table dispatch |
| Equivalence | static func is equivalent to class final func | class func |
Code Example:
class Vehicle {
// static stored property (lazy & thread-safe)
static let defaultWheelCount = 4
// static method — CANNOT be overridden
static func generalInfo() -> String {
return "Vehicles are used for transportation."
}
// class computed property — CAN be overridden
class var category: String {
return "Generic Vehicle"
}
// class method — CAN be overridden
class func maxSpeed() -> Int {
return 120
}
}
class SportsCar: Vehicle {
// ❌ Error: Cannot override static method
// override static func generalInfo() -> String { ... }
// ✅ OK: Overriding class computed property
override class var category: String {
return "High Performance Vehicle"
}
// ✅ OK: Overriding class method
override class func maxSpeed() -> Int {
return 300
}
}
Key Points:
staticis available across structs, enums, actors, and classes;classis strictly for class types.static let / varstored properties are lazily initialized on first access and guaranteed thread-safe (viadispatch_onceunder the hood).- In protocols, type requirements are always declared using
static. Conforming classes can implement them with eitherstatic(to prevent subclass overrides) orclass(to allow subclass overrides). - Prefer
staticby default unless you explicitly intend for subclasses to override the behavior.
Further reading
- Methods: Type Methods — The Swift Programming Languagedocs.swift.org (opens in a new tab)
- Properties: Type Properties — The Swift Programming Languagedocs.swift.org (opens in a new tab)
- What's the difference between a static variable and a class variable? — Hacking with Swifthackingwithswift.com (opens in a new tab)
- What is the difference between static func and class func in Swift? — Stack Overflowstackoverflow.com (opens in a new tab)
- The difference between static vs class function in Swift — Mediummedium.com (opens in a new tab)