Swift
Programming
Static Constants
Swift Development
Swift Classes

How to define static constant in a class in swift

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

A static constant in Swift is a value that belongs to the type itself rather than to any individual instance. It is useful for shared configuration, fixed identifiers, and namespaced values that should never change while the app runs.

Define a Static Constant with static let

Inside a class or struct, use static let.

swift
1final class APIConfig {
2    static let baseURL = "https://api.example.com"
3    static let requestTimeout: TimeInterval = 30
4}
5
6print(APIConfig.baseURL)
7print(APIConfig.requestTimeout)

Because the properties are static, you access them through the type name. There is no need to create an APIConfig instance first.

Why Use a Static Constant

A static constant is useful when all instances should see the same immutable value.

swift
final class Theme {
    static let primaryColorName = "OceanBlue"
}

That communicates intent clearly: this is metadata about the type or a globally shared constant, not per-object state.

Typical uses include:

  • API endpoint roots
  • notification names
  • default numeric limits
  • reusable layout constants
  • identifiers for persistence keys

Difference Between let, static let, and class var

These forms solve different problems.

An instance constant belongs to one object:

swift
1final class User {
2    let username: String
3
4    init(username: String) {
5        self.username = username
6    }
7}

A static constant belongs to the type:

swift
final class User {
    static let maxNameLength = 20
}

A class var is a type property that subclasses can override, but it is computed rather than stored in the same way as static let.

swift
1class Vehicle {
2    class var category: String {
3        "generic"
4    }
5}
6
7final class Car: Vehicle {
8    override class var category: String {
9        "car"
10    }
11}

If the value is fixed and should not be overridden, static let is usually the best fit.

Static Constants Are Lazily Initialized

Swift initializes static stored properties lazily the first time they are accessed. That means you can safely compute them from other values.

swift
1final class Paths {
2    static let cacheDirectory: String = {
3        let root = NSTemporaryDirectory()
4        return root + "app-cache"
5    }()
6}
7
8print(Paths.cacheDirectory)

This is convenient for setup code because the property is initialized once and then reused.

Using Static Constants for Namespacing

A static constant also helps group related values without polluting the global namespace.

swift
1enum AnalyticsEvent {
2    static let login = "login"
3    static let logout = "logout"
4    static let purchase = "purchase"
5}

Even though the article title mentions classes, the same pattern often works well in enums or structs used purely as namespaces.

Access Control Still Applies

You can keep a static constant internal, private, or public like any other property.

swift
1final class Secrets {
2    private static let apiKey = "development-only-key"
3
4    static func authorizationHeader() -> String {
5        "Bearer \(apiKey)"
6    }
7}

That allows you to expose behavior while hiding implementation details.

Prefer Meaningful Names Over Magic Numbers

Static constants are a good way to replace hardcoded values spread throughout a codebase.

swift
1final class LayoutMetrics {
2    static let cardCornerRadius: CGFloat = 12
3    static let horizontalPadding: CGFloat = 16
4    static let avatarSize: CGFloat = 48
5}

Using named constants makes layout code easier to review and change later.

Common Pitfalls

A common mistake is trying to access a static constant through an instance instead of through the type name. Another is using class var when the value is actually fixed and should not be overridden. Developers also sometimes store unrelated app-wide constants in a random class, which makes the codebase harder to navigate. Finally, a static constant can still hold mutable reference types, so immutability of the property does not automatically make the referenced object deeply immutable.

Summary

  • Define a static constant with static let.
  • Access it through the type name, not an instance.
  • Use it for shared immutable values and namespacing.
  • Prefer static let over class var when overriding is not needed.
  • Replace scattered magic numbers and string literals with clearly named type-level constants.

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.