Class and instance attributes

What Are the Advantages and Drawbacks of Class and Instance Attributes?

  • They store data that is relevant to all the instances. For example, we could have a counter class attribute that increments every time we create a new instance and decrements every time we delete an instance. This way we can always keep track of how many instances of the same class we created.
  • you can’t use them to do different things on different objects. if our program countains only class attributes we would not be able to make a list of different objects in it.
  • They are specific to an object and are easy to set and get thanks to properties.
  • They are discarded once the instance is deleted, so they die with the instance they are associated with, which makes things clearer.
  • They don’t allow keeping track of values in between instances.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store