---
title: "Objective C : object creation method explanations"  
description: "Previously we learn about Headers, interfaces, methods of Objective C : Objective C : Headers, Interfaces, Methods     Creating a new object in Object"  
author: "Tarun Kumar"  
published: 2015-07-06  
updated: 2018-02-24  
canonical: https://www.mindstick.com/blog/10915/objective-c-object-creation-method-explanations  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 2 minutes  

---

# Objective C : object creation method explanations

Previously we learn about Headers, interfaces, methods of [Objective C](https://www.mindstick.com/articles/1806/objective-c-headers-interfaces-methods) : Objective C : [Headers, Interfaces, Methods](https://www.mindstick.com/Articles/1806/objective-c-headers-interfaces-methods#.VZzI81i6bIU) \

\

Creating a new object in Objective-C is usually a two-step process. First, memory has to be allocated for the object, then the object is initialized with proper values.

Alloc : alloc allocates a chunk of memory to hold the object, and returns the pointer.

```
MyClass* myObj = [MyClass alloc];
```

§ myObj cannot be used yet because its internal state is not correctly setup. So, don't write a code like this.

Init : init [sets up](https://answers.mindstick.com/qa/62167/which-state-government-sets-up-a-skill-university-with-a-cost-of-850-crores) the initial [condition](https://www.mindstick.com/forum/12711/select-query-with-and-condition) of the object and returns it.

```
-init {           self = [ super init ] ; // 1.         If ( self ) { // 2.              ....         } return self ; // 3.}
```

**1.** First, you need to call the superclass's init, to setup the superclass's [instance variables](https://www.mindstick.com/forum/34631/instance-variables-and-class-variables), etc. That might return something not equal to the original self, so you need to assign what's returned to self.

**2.** If self is non-nil, it means the part [controlled](https://yourviews.mindstick.com/view/81837/has-pakistan-controlled-corona-pandemic) by the superclass is correctly initialized. Now you perform your [initialization](https://www.mindstick.com/interview/160/what-is-the-difference-between-assignment-and-initialization). All of the instance variables are set to nil (if it's object) and 0 if it's integer. You'll need to perform additional initial settings.

**3.** Return the set-up self. The returned self might be different from what's allocated! So, you need to assign the result of init to your variable.

Never split the call to [alloc and init](https://www.mindstick.com/forum/33420/objective-c-allow-to-allocate-object-through-alloc-and-init). Don't write:

```
MyClass* myObj = [MyClass alloc];[myObj init];
```

because [myObj init] might return [something else](https://www.mindstick.com/forum/1654/can-anybody-say-my-below-code-is-a-variable-function-or-something-else).

Don't try to get around this by writing:

```
MyClass* myObj = [MyClass alloc];myObj=[myObj init];
```

because you will eventually forget to write the part myObj= in the second line.

Always write:

```
MyClass* myObj = [[MyClass alloc] init];
```

I also don't recommend writing:

```
MyClass* myObj = [MyClass new];
```

because it does not correctly call the initialization method: some classes does not accept a plain init. For example, NSView needs initWithFrame:, which can't be called with new. So, don't use new either.

\

Next, we will learn about : [Objective-C Program Structure](https://www.mindstick.com/Articles/1808/objective-c-program-structure)

---

Original Source: https://www.mindstick.com/blog/10915/objective-c-object-creation-method-explanations

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
