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
-
Dup: http://stackoverflow.com/questions/1053592/objective-c-class-vs-instance-methods – Jul 24 '10 at 03:05
2 Answers
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.

- 75,757
- 21
- 156
- 151
-
Thanks for that. The second question is not clear, I've just asked if I have only one instance in program (I don't need some more) do I need to create the instance or I can use class and remake all the instant methods like class methods. – moldov Jul 24 '10 at 04:42
-
moldov: Either will work, but it is much more common to create a single instance and have a `+[Something sharedSomething]` method that returns the single instance. – rpetrich Jul 24 '10 at 06:24
Please visit Cocoa Design Pattern Singleton for single instance of class.
-
Thanks. Actually I'm noob in OOP, so the patterns stuff should be useful for me. I'll read through – moldov Jul 24 '10 at 04:45