Ask Question
18 December, 15:22

Explain how class (static) variables and methods differ from their instance counterparts. Give an example of a class that contains at least one class variable and at least one class method. Explain why using a class variable and method rather than an instance variable and method would be the correct choice in the example you select.

+2
Answers (1)
  1. 18 December, 15:51
    0
    Static variables and functions differs from their instance counterparts in that you don't need to instantiate an object of a class to access them (variables) or call them (functions). The class itself can be used to access or call static variables and functions.

    Explanation:

    Example:

    Suppose we have a class that represents a word editor. To save progress, we need a file location on the disk.

    We can define file location as a static variable, since all instances of the class will use the same file location value.

    Also, to access the static variable outside of class definition, we can define a static method to get/set the value of the static variable.

    A static save to file function can also be defined in the class (to perform the work to update the file on disk). Again this function would be defined as static, because all instances of the class will need the same method whenever they want to write to file.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Explain how class (static) variables and methods differ from their instance counterparts. Give an example of a class that contains at least ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers