Language > Objects > Creating Object Classes > Creating Static Methods

# Creating Static Methods

IDL object classes can have both instance methods and static methods.

When you create a method on a class, by default the method is an "instance" method. Instance methods can only be called on an actual "instance" of that class. Within an instance method, you have access to the `self` variable and the object's instance data.

However, you can also mark a method as "static" by using the `compile_opt static` directive at the top of the routine. When a static method is called, the normal object lifecycle is bypassed: the ::`Init` and ::`Cleanup` methods are not called, and the `self` variable will be set to a null object.

Note: Marking a method as "static" just means that you are allowed to call that method as a static method. You can also call the same method on an instance of that object. In that case, just like any other object, the `self` variable will be defined and will contain the object's instance data. If a method is not marked with the `compile_opt static` directive , then trying to call it as a static method will result in a runtime error.

## Static Method Calls versus Array Indexing

In IDL, by default both square brackets [ ] and parentheses ( ) can be used for array indexing. However, parentheses are also used for function calls. Because of this duplication, the IDL compiler cannot distinguish between function calls and array indexing. For example, you might have a static method call:

Result = MyClass.Calculate(a, b)

However, it is just as likely that you might be doing an array index into a structure:

Result = MyStruct.Field(a, b)

By default, IDL will be unable to distinguish between these two calls and in certain cases will throw a syntax error. To avoid any confusion, you should always use `compile_opt strictarr` (or `compile_opt idl2`) at the top of your procedures and functions. This will force you to use square brackets for all array indexing within that routine. The IDL compiler will then treat any parentheses as function calls.

### Use of Static Methods at the Main Program Level

Normally, if you are doing static method calls within a routine, it is straightforward to insert `compile_opt strictarr` to avoid any confusion with parentheses. However, if you are executing ad-hoc IDL commands at the main program level, it might be confusing to suddenly receive syntax errors when trying to make static method calls.

Because of this issue, if you use a static method call at the main program level (either at the command prompt or in a $MAIN program), IDL will then automatically turn on `compile_opt strictarr` for the main level. From that point on, you must then be sure to use square brackets for array indexing. See compile_opt for details.

## See Also

Static Methods