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.

Featurestaticclass
Can be overridden?No (implicitly final)Yes (supports polymorphism)
Supported typesclass, struct, enum, actor, protocolclass only (and class-bound protocols)
Stored propertiesYes (static let / static var)No (computed properties only)
Dispatch mechanismStatic / direct dispatch (fast)Dynamic / table dispatch
Equivalencestatic func is equivalent to class final funcclass 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:

  • static is available across structs, enums, actors, and classes; class is strictly for class types.
  • static let / var stored properties are lazily initialized on first access and guaranteed thread-safe (via dispatch_once under the hood).
  • In protocols, type requirements are always declared using static. Conforming classes can implement them with either static (to prevent subclass overrides) or class (to allow subclass overrides).
  • Prefer static by default unless you explicitly intend for subclasses to override the behavior.