Compare the Difference Between Similar Terms

Difference Between Static and Non Static Method

Static vs Non Static Method

A method is a series of statements that is carried out to perform a specific task. Methods can take inputs and produce outputs. Static and non static methods are two types of methods present in object oriented programming languages. A static method is a method that is associated with a class. A method that is associated with an object is called a non static (instance) method. In object oriented languages, methods are used as a mechanism to operate on data that are stored in objects.

What is a Static Method?

In object oriented programming, static method is a method that is associated with a class. Therefore, static methods do not have the capability to operate on a particular instance of a class. Static methods can be invoked without using an object of the class that contains the static method. Following is an example of defining a static method in Java. The static has to be used when defining a static method in Java.

public class MyClass
{
public static void MyStaticMethod()
{
// code of the static method
}

}

The static method defined above could be called as follows using the name of the class it belongs to.

MyClass.MyStaticMethod();

One important thing to note is that static methods can only access static members.

What is a Non Static Method?

A non static method or an instance method is a method that is associated with an object in a class. Therefore, non static methods are called using an object of the class in which the method is defined. A non static method can access non static members as well as static members of a class. In many object oriented languages (such as C++, C#, Java), when a non static method is called, the object that invoked the method is passed as an implicit argument (it is called the ‘this’ reference). So, inside the method this keyword can be used to refer to the object that called the method. Following is an example of defining an instance method in Java.

public class MyClass
{
public void MyInstanceMethod()
{
// code of the instance method
}

}

The instance method defined above could be called as follows using an object of the class it belongs to.

MyClass objMyClass = new MyClass();

objMyClass.MyInstanceMethod ();

What is the difference between Static and Non Static Method?

Static methods are methods that are associated with a class, whereas non static methods are methods that are associated with objects of a class. A class needs to be instantiated first to invoke a non static method, but static methods do not have this requirement. They can be simply invoked using the name of the class that holds the static method. Another important difference is that a non static method usually possesses a reference to the object that called the method and it can be accessed using the this keyword inside the method. But this keyword cannot be used in static methods since they are not associated with a particular object.