Question: I'm finding it difficult to understand when it is necessary to create class methods. From what I've read, they are important for creating new objects, but I do not see how. The following class create a simple shape black rectangle. Can anyone show me how to incorporate a class method to do something that I could not do with an instance method?

Shape.h

```
#import <UIKit/UIKit.h>
@interface Shape : UIView; 
- (id) initWithX: (int)xVal andY: (int)yVal;
@end

```

Shape.m

```
#import "Shape.h"
@implementation Shape 
- (id) initWithX:(int )xVal andY:(int)yVal {
self = [super init];    
UIView *shape = [[UIView alloc] initWithFrame:CGRectMake(xVal, yVal, 10, 10)];
shape.backgroundColor = [UIColor blackColor];
[self addSubview:shape];
return self;
}
@end

```

Answer: 
Class methods have several uses:

* As you've heard, they're essential for creating new objects. Creating objects is a two step process: first, you allocate memory for the object using the `+alloc` **class** method, then you initialize the object using some **instance** method that usually begins with `-init`. The `+alloc` method must be a class method because you don't have an instance upon which to call it; you only know the class name. The `-init` method should be an instance method, because `+alloc` returns an instance, an you want to initialize the properties unique to that instance, not to the entire class. So, you used a class method then an instance method in your code above:
`UIView *shape = [[UIView alloc] initWithFrame:CGRectMake(xVal, yVal, 10, 10)];`
* Class methods are useful for retrieving singletons, or shared instances of a class. For example, the `NSUserDefaults` class stores preferences and settings associated with your app. In most cases, people like to use a single set of preferences for their entire app, so it has a class method `+standardUserDefaults` that returns a single instance of `NSUserDefaults`that everyone can use at any point in the app's lifetime to get all the preferences associated with an app. Without that singleton and class method, you'd have to create and pass around an instance of `NSUserDefaults` throughout your app, which gets messy. Other examples include `+[UIDevice currentDevice]` and `+[NSFileManager defaultManager]`.
* Class methods are great for returning data or properties shared by all instances of the class. For example, `UIView` has a class method `+layerClass` that returns the kind of layer the view hosts. Since all instances of a particular `UIView` subclass use the same kind of layer, it has nothing to do with individual instances and thus makes sense as a class method.
* You'll see a lot of class methods called "convenience methods." These methods (generally) combine calls to `+alloc` and `-init` and return an autoreleased instance of a class. For example, you can save some typing by writing `[NSArray arrayWithObjects:...]` instead of `[[[NSArray alloc] initWithObjects:...] autorelease]`. If you're using ARC, these convenience methods are less useful, but they are still everywhere in Cocoa.
* Class methods are also sometimes used for utility classes, or classes that don't have state, and just have a bunch of methods that take in all the parameters they need. For example, you might have a method like `+[PhoneUtilities parseAreaCodeFromPhoneNumber:string]`. There are not many of these methods built-in to Cocoa; some people argue that it's better to make them regular C functions, singletons, or regular objective-c classes with instance methods.

I'm sure there are more, but that should give you some ideas.
My answer would be that class methods are very useful when you need to do some utility functions / methods and *don't* need to be doing actions on some specific instance or object. 

For example, in my own code, I typically do class methods in classes where the outcome will be the same no matter which object I could possibly call the method on. Like:

E.G. 

```
@interface MyAppCacheFiles
    + (NSURL *) getFileURLOfLocalCache;
@end

```

These could also be considered convenience methods (e.g. much like `NSString +stringWithFormat:` or `NSArray +arrayWithArray:`).