Swift
Programming
Static vs Class
Functions
Variables

Static vs class functions/variables in Swift classes?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Swift, both static and class define type-level members, meaning they belong to the type itself rather than to an instance. The difference is not about storage location or syntax preference. The real difference is whether subclasses are allowed to override that member later.

What static Means

Use static for type properties and type methods that should not be overridden.

swift
1class Logger {
2    static let category = "network"
3
4    static func format(_ message: String) -> String {
5        "[\(category)] \(message)"
6    }
7}
8
9print(Logger.category)
10print(Logger.format("connected"))

Important characteristics:

  • accessed on the type, not an instance
  • cannot be overridden in a subclass
  • available on classes, structs, and enums

If you know the behavior should stay fixed across the inheritance chain, static is the right keyword.

What class Means

Use class only on class members that are allowed to be overridden by subclasses.

swift
1class Vehicle {
2    class func description() -> String {
3        "Generic vehicle"
4    }
5}
6
7class Car: Vehicle {
8    override class func description() -> String {
9        "Car"
10    }
11}
12
13print(Vehicle.description())
14print(Car.description())

This is useful when a base class wants to define a type-level API but still let subclasses customize it.

The key rule is simple:

  • 'static means no override'
  • 'class means subclass override is allowed'

Type Properties in Swift Classes

Type properties follow a slightly stricter rule than methods:

  • stored type properties use static
  • overridable type properties must be computed and use class

Stored property example:

swift
class Settings {
    static var appName = "Codemia"
}

Computed property example that subclasses can override:

swift
1class Shape {
2    class var displayName: String {
3        "Shape"
4    }
5}
6
7class Circle: Shape {
8    override class var displayName: String {
9        "Circle"
10    }
11}

You cannot declare an overridable stored type property with class var value = .... If the property is meant to vary by subclass, make it computed.

When to Choose Each One

Use static when:

  • the behavior is fixed
  • inheritance customization is not part of the design
  • the member is shared configuration or a utility method

Use class when:

  • the base class defines a polymorphic type-level API
  • subclasses should provide specialized behavior
  • you are modeling factories, metadata, or template-like hooks

For example, a factory pattern often benefits from class func:

swift
1class Document {
2    class func fileExtension() -> String {
3        "txt"
4    }
5}
6
7class Spreadsheet: Document {
8    override class func fileExtension() -> String {
9        "xlsx"
10    }
11}

That is a clean way to let subclasses customize type-specific metadata.

Why static Often Wins by Default

If you do not need overriding, prefer static. It expresses intent more clearly and prevents unnecessary inheritance coupling.

Many shared constants and helpers are not meant to vary:

swift
1class API {
2    static let timeoutSeconds = 30
3
4    static func baseURL() -> String {
5        "https://api.example.com"
6    }
7}

Making those class members would suggest extensibility that the design does not actually need.

Common Pitfalls

The most common mistake is assuming class is simply the class-only version of static. It is narrower and more specific than that: it means "this member is a type member and subclasses may override it."

Another mistake is trying to create an overridable stored type property. In Swift, subclass-overridable type properties must be computed.

Some developers also overuse inheritance for what should just be namespaced helper methods. If there is no real polymorphism, static is usually cleaner.

Finally, remember that static is the only option for structs and enums. class applies only to classes because override behavior only exists in class inheritance.

Summary

  • Both static and class create type-level members in Swift.
  • 'static members cannot be overridden.'
  • 'class methods and computed properties can be overridden by subclasses.'
  • Stored type properties use static, not overridable class storage.
  • Prefer static unless you specifically need subclass customization.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.