
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
What is the difference between Static func and Class func in Swift?
You will learn about the differences between static and class functions in this article.
Many times, you might think they are the same thing, but that's not the case. Let's understand the difference between them.
What are Static Functions?
In Swift, a static function is a type of function that is associated with a type and not with an instance. This means that an instance is not required to call a static function as it can be called from the type itself.
Example
Here's an example of how you might define a static function in Swift −
struct ViewPoint { static func defaultOrigin() -> ViewPoint { return ViewPoint(x: 0, y: 0) } var x: Int var y: Int } let origin = ViewPoint.defaultOrigin() print(origin)
Output
ViewPoint(x: 0, y: 0)
Explanation
In the above example, we create a struct called ViewPoint with some properties like x and y. In the ViewPoint struct, we define a static function called defaultOrigin() to get the default origin value.
You can see, we called the function defaultOrigin() with the type itself without creating an instance of that type.
What are Class Functions?
In Swift, a class function is a type of function that is associated with a class and not with an instance. This means that you can call a class function on the class itself, rather than on an instance of the class.
Example
Here's an example of how you might define a class function in Swift −
class ViewPoint { class func defaultOrigin() -> ViewPoint { return ViewPoint(x: 0, y: 0) } var x: Int var y: Int init(x: Int, y: Int) { self.x = x self.y = y } } let origin = ViewPoint.defaultOrigin() print("x: \(origin.x), y: \(origin.y)")
Output
x: 0, y: 0
Explanation
In the above example, we create a class called ViewPoint with some properties like x and y. In the ViewPoint class, we define a class function called defaultOrigin() to get the default origin value.
You can see, we called the function defaultOrigin() with the type itself without creating an instance of that type.
Why do we Need Static or Class Functions?
Whenever we write code in an application, we might need to include some utility functions that perform utility tasks. You can define those methods as a static or class function to call them without creating an instance of that type.
What are The Differences Between Them?
A static function can be associated with a type like a struct, class, or enum, while a class function is associated with a class type only.
Static functions are defined using the static keyword, while class functions are defined using the class keyword.
Class functions can be overridden by subclasses, while static functions cannot be overridden.
Class functions can access the self properties of the class, which is not possible for static functions.
Example
Here's an example of how you can override a class function in Swift −
class ViewPoint { class func defaultOrigin() -> ViewPoint { return ViewPoint(x: 0, y: 0) } var x: Int var y: Int init(x: Int, y: Int) { self.x = x self.y = y } } class RectangleView: ViewPoint { override class func defaultOrigin() -> ViewPoint { return ViewPoint(x: 1, y: 1) } } let originView = ViewPoint.defaultOrigin() print("View Origin -> x: \(originView.x), y: \(originView.y)") let originRectangle = RectangleView.defaultOrigin() print("Rectangle Origin -> x: \(originRectangle.x), y: \(originRectangle.y)")
Output
View Origin -> x: 0, y: 0 Rectangle Origin -> x: 1, y: 1
Explanation
In this example,
Step 1 − The ViewPoint class has a class function called defaultOrigin() and a static function called staticOrigin() which returns a ViewPoint instance with an x and y value of 0.
Step 2 − The RectangleView class is a subclass of ViewPoint, and it overrides the defaultOrigin() class function to return a ViewPoint instance with an x and y value of 1.
You can see the function staticOrigin() that is overridden in the subclass RectangleView has been disabled by comment. You can remove the comments and run this code and you will get to see an error like "error: cannot override static method".
Difference Between Static And Class Functions
Static Functions |
Class Functions |
---|---|
Can be associated with a class, struct, or enum. |
Can be associated with class only. |
Defined by static keyword. |
Defined by class keyword. |
Can not be overridden in the subclass. |
Can be overridden in the subclass. |
Can not access self properties |
Can access self properties |
Conclusion
We can use static or class functions to define utility tasks in your application. Since they are not instance dependent, they can be called directly from the type itself.
- Related Articles
- What is the difference between Func delegate and Action delegate in C#?
- Func generic type in C#
- What is the difference between class and structure in Swift?
- What is the difference between Static class and Singleton instance in C#?
- Golang program that uses func with two arguments
- Golang Program that uses func as Local Variable
- Golang program that uses func with variable argument list
- What is the difference between static and dynamic polymorphism?
- What is the difference between static classes and non-static inner classes in Java?
- What are the differences between a static and a non-static class in C#?
- What is the difference between Component class and Container class in Java?
- What is the difference between public, static and void keywords in C#?
- What is the difference between static friction force and potential energy ?
- What is the difference between abstract class and a concrete class in Java?
- What is the difference between non-static methods and abstract methods in Java?
