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.
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.
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.
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:
- '
staticmeans no override' - '
classmeans 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:
Computed property example that subclasses can override:
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:
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:
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
staticandclasscreate type-level members in Swift. - '
staticmembers cannot be overridden.' - '
classmethods and computed properties can be overridden by subclasses.' - Stored type properties use
static, not overridableclassstorage. - Prefer
staticunless you specifically need subclass customization.
Related reading
- Static way to get 'Context' in Android?
- Static way to get 'Context' in Android?
- Status bar and navigation bar appear over my view's bounds in iOS 7
- Status bar height in Swift
- Status bar height in Swift
- Still getting warning Configuration 'compile' is obsolete and has been replaced with 'implementation
- Stop UIWebView from bouncing vertically?
- Stopping an Android app from console
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.