views:

60

answers:

2

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

A: 

This seems to be several questions:

  1. What is the difference between a class and an instance of a class?
  2. What if I can only use one instance of the class?
  3. 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.

Rafe Kettler
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
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
A: 

Please visit Cocoa Design Pattern Singleton for single instance of class.

shiki
Thanks. Actually I'm noob in OOP, so the patterns stuff should be useful for me. I'll read through
moldov