Could someone explain to me what is the difference between class and instance of the class. If I can use only one instance of the some class in the program, can I use the class like an instance and change all the (-) with (+) in the methods declaration. What is the difference between the class and instance methods. Thanks
This seems to be several questions:
- What is the difference between a class and an instance of a class?
- What if I can only use one instance of the class?
- What is the difference between class and instance methods?
First, the difference between a class and an instance of a class is that a class is a specification for an instance. The class will always be there, but you must create an instance to use the instance methods of that class. The class is what creates instances and gives them methods.
Second, "if I can use only one instance of a class in a program" is a situation that will never come up. You can make as many instances of a class as you want (hardware permitting).
Third, the difference between a class and an instance method is that while you must create an instance to use an instance method, class methods are just useful functions that the class offers without creating an object from that class. Instance methods operate on the properties/fields of specific instances, while class methods merely accept input and return values independent of any instances.
Please visit Cocoa Design Pattern Singleton for single instance of class.