Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between Static and Non Static Method

Difference Between Static and Non Static Method

July 14, 2011 Posted by Indika

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.

Related posts:

Difference Between Structured Programming and Object Oriented Programming Difference Between Object and Instance Difference Between AOP and OOP Difference Between Object Oriented Programming and Procedural Programming Difference Between Encapsulation and Abstraction

Filed Under: Programming Tagged With: Class, instance method, method, non static method, object, OOP, statements, static method

About the Author: Indika

Indika, BSc.Eng, MSECE Computer Engineering, PhD. Computer Science, is an Assistant Professor and has research interests in the areas of Bioinformatics, Computational Biology, and Biomedical Natural Language Processing.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Endocytosis and Exocytosis

Difference Between Endocytosis and Exocytosis

Difference Between Sensory and Motor Neurons

Difference Between Sensory and Motor Neurons

Difference Between MRT and LRT

Difference Between Lenovo IdeaTab S2 and Asus Eee Pad Transformer Prime

Difference Between Nokia 500 and Nokia 700

Difference Between Nokia 500 and Nokia 700

Latest Posts

  • What is the Difference Between Herniated Disc and Piriformis Syndrome
  • What is the Difference Between Spinal Stenosis and Spondylosis
  • What is the Difference Between Lip Flip and Lip Filler
  • What is the Difference Between Bone Spurs and Plantar Fasciitis
  • What is the Difference Between Tension Pneumothorax and Cardiac Tamponade
  • What is the Difference Between Diverticulitis and Crohn’s Disease
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.