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

2011年7月14日Posted byIndika

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 ();

之间的区别是什么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:ProgrammingTagged 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 ReplyCancel 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 Megabyte Gigabyte and Terabyte

Difference Between Megabyte Gigabyte and Terabyte

Difference Between Pharmacist and Pharmacy Technician

Difference Between History and Legend

Difference Between History and Legend

Difference Between Yoga and Exercise

What is the Difference Between Vacuum Pump and Compressor

What is the Difference Between Vacuum Pump and Compressor

Latest Posts

  • What is the Difference Between Work From Office and Work From Home
  • What is the Difference Between Celiac Disease and Lactose Intolerance
  • What is the Difference Between Coal Tar and Asphalt
  • What is the Difference Between Inflammation and Swelling
  • What is the Difference Between Sandy Soil and Loamy Soil
  • What is the Difference Between Spleen and Liver
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

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