# Declaration of variables in different locations in Objective-C

Having worked with Objective-C you will have noticed that there are many different locations to declare instance variables and local variables. In this article, we are going to explore the different possibilities and the meaning of each case.

#import &lt;Foundation/Foundation.h&gt;

// 1. Declare something outside the interface

@interface myClass : NSObject
{
// 2. Declare something inside the curly braces
}

// 3. Declare something inside the interface but not inside the curly braces

@end


In the first case we usually define typedefs, enums or externs. It is not a good idea to declare variables at this location. However, if we decide to declare variables, those are C-style variables and work exactly the same. In this location, since the scope is file wide, they will be available to every one that imports this header.

The second location was used in the past to declare instance variables. However, nowadays we prefer to declare properties and avoid to synthesize or implement manually the accessor methods. Therefore, the second location exist only for compatibility if for some specific reason we want to declare the ivars of the class. The advantages of using properties instead manual declaration of instance variables are that the memory management and the accessors methods as well as the dot-notation easy use.

In the third location we declare the properties and the public methods.

Now, lets look in the implementation file. It will look like this:

#import &quot;myClass.h&quot;

// 4. Declare something outside the implementation

@interface myClass()
// 5. Declare something inside the class extension
@end

@implementation myClass
{
// 6. Define something inside the curly braces inside the implementation part
}

// 7. Define something inside the implementation part

@end



At the forth location you can define external constants as well as static variables which plays the role of the class variables.

Inside the private interface, in the fifth location, you can define private methods and properties. It is a good habit to declare inside this private interface whatever has to be kept away from the public.

In the sixth place you can define instance variables which are private to the class. Usually, you declare variables that are not objective-c variables such as C++ or C-style vars.

In the seventh place you can define methods or synthesize properties. Since Xcode 4.4 you don’t need to synthesise the properties aside from the case that you want to change the name of the instance variable other than the default _propertyName.