dmyma
dmyma

Reputation: 223

static vs. final in Swift

What is the difference between static and final keywords in Swift? When should each be used?

I understand that for a static Class nothing can inherit from it, but that is also true for final.

Thank you

Upvotes: -2

Views: 7117

Answers (3)

Dhaval H. Nena
Dhaval H. Nena

Reputation: 4160

Common thing between Static and Class is, that they both attaches variables to the Class itself and not to the instance of class.

Where they differ is, you CAN OVERRIDE class properties whereas in case of static you CAN NOT OVERRIDE it in subclass.

Hope this this helps.

Upvotes: 1

Hula
Hula

Reputation: 81

  • static: used for properties or functions of class or struct and can be accessed by class/struct level. With static keyword, we cannot override.
  • final: used for class and class members (properties or functions). With final keyword, we cannot override.

Note: static is same with "final class" for class members

Upvotes: 1

dmyma
dmyma

Reputation: 223

So, after a while research I would say that static is used for a property that can be accessed without created a class instance and final is a modifier for a class that from this class nobody can be inherent.

Upvotes: 8

Related Questions