Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Diffrence between instance and static method?

Gaku
Explorer
9,228

What is the difference between static and instance method?

When and why instance method is used ?

1 ACCEPTED SOLUTION
Read only

michaelf_
Product and Topic Expert
Product and Topic Expert
7,672

Dear Gakuto,

the terms "instance" and "static" are general terms from the Object Orientation (OO) programming paradigm.

Imagine, you are a car manufacturer. You would want to track the mileage per car and also the number of cars produced in total. For this scenario, we specify the class "Car". In this class, we specify a static attribute "NumberOfCarsProduced" and a static method "IncreaseNumberOfCarsProduced". Whenever we create a new instance of the class "Car", we increase the number of cars produced in total. The number of cars produced in total is of course static. For the instance, we specify an instance attribute "Mileage" and an instance method "IncreaseMileage". Whenever we drive with a car instance, we increase the mileage. The mileage of a car instance is an instance attribute.

With kind regards,

Michael

5 REPLIES 5
Read only

michaelf_
Product and Topic Expert
Product and Topic Expert
7,673

Dear Gakuto,

the terms "instance" and "static" are general terms from the Object Orientation (OO) programming paradigm.

Imagine, you are a car manufacturer. You would want to track the mileage per car and also the number of cars produced in total. For this scenario, we specify the class "Car". In this class, we specify a static attribute "NumberOfCarsProduced" and a static method "IncreaseNumberOfCarsProduced". Whenever we create a new instance of the class "Car", we increase the number of cars produced in total. The number of cars produced in total is of course static. For the instance, we specify an instance attribute "Mileage" and an instance method "IncreaseMileage". Whenever we drive with a car instance, we increase the mileage. The mileage of a car instance is an instance attribute.

With kind regards,

Michael

Read only

matt
Active Contributor
7,672

You should rarely use static methods.

Read only

Tanishaa
Participant
0 Likes
7,672

Hi aiueo

The main difference between the two is that instance method can be called using object reference only and hence is dependent on object while the static method can be called irrespective of the object for a class, it can be called using class name and hence is not dependent on object.
Static method can access only static attributes while instance method can access static as well as instance attributes. You can declare a static method using 'METHODS' keyword and instance method can be declared using keyword 'CLASS-METHODS'.

Read only

0 Likes
7,672

Static Methods: these methods can be called without creating an object for the class. these methods can be directly called using the class name like

cl_gui_frontend_services=>gui_download().

Instance Methods: These methods can be only called using the object of the class.