Static func vs class func in Swift: What's the Difference?

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Static func vs class func in Swift: A Comprehensive Guide for Developers

\n\n

Anyone who writes in Swift inevitably faces the dilemma: static func or class func? At first glance, both keywords serve to create type-level methods. But the difference between them is not just a syntactic whim, but a fundamental distinction related to inheritance and polymorphism. In this article, we will once and for all understand how static func differs from class func in Swift, when to use each, and how this knowledge will help you write cleaner and more flexible code.

\n\n

What are Type Methods in Swift?

\n\n

Before diving into the differences, let's recall the basics. In Swift, methods are divided into two types:

\n\n
    \n
  • Instance methods — called on a specific object of a class, structure, or enumeration.
  • \n
  • Type methods — called on the type itself, not on its instance. The keywords static or class are used to declare them.
  • \n
\n\n

Type methods are ideal for factory methods, singletons, utility functions, and configurations that are not tied to a specific object state.

\n\n

Static func: The Main Guardian of Immutability

\n\n

static func is a method that cannot be overridden in a subclass. It belongs exclusively to the type in which it is defined. The static keyword works the same for classes, structures, and enumerations.

\n\n

Example: static func in a Class

\n\n
class Animal {\n    static func describe() {\n        print("I am an animal. My essence cannot be changed.")\n    }\n    \n    func speak() {\n        print("Making a sound")\n    }\n}\n\nclass Dog: Animal {\n    // Error! Cannot override a static method\n    // override static func describe() { }\n    \n    override func speak() {\n        print("Woof-woof!")\n    }\n}\n\n// Usage\nAnimal.describe()  // "I am an animal. My essence cannot be changed."\nDog.describe()     // Also "I am an animal. My essence cannot be changed."\n
\n\n

Note: Dog.describe() calls the parent class method. Although technically the subclass inherits the static method, it cannot be overridden. This behavior resembles final class func.

\n\n

Static func in Structs

\n\n

Structures (value types) also support static methods:

\n\n
struct MathHelper {\n    static func square(_ number: Int) -> Int {\n        return number * number\n    }\n}\n\nlet result = MathHelper.square(5)  // 25\n
\n\n

Since structures do not support inheritance, static here is the only possible way to declare a type method.

\n\n

Class func: Flexibility of Inheritance

\n\n

class func is a type method that can be overridden in a subclass. It is available only for classes (reference types). Structures and enumerations cannot use class.

\n\n

Example: class func and its Override

\n\n
class Vehicle {\n    class func makeNoise() {\n        print("The vehicle makes noise")\n    }\n    \n    class final func isMotorized() -> Bool {\n        return true\n    }\n}\n\nclass Car: Vehicle {\n    override class func makeNoise() {\n        print("The car hums: vroom vroom!")\n    }\n    \n    // Error! Cannot override a final class func\n    // override class func isMotorized() -> Bool { return false }\n}\n\n// Usage\nVehicle.makeNoise()  // "The vehicle makes noise"\nCar.makeNoise()      // "The car hums: vroom vroom!"\n
\n\n

As you can see, class func behaves like a regular virtual method, but is called at the type level. This opens up possibilities for polymorphism at the class level.

\n\n

Blogs

Book Recommendations