Dart Methods

In this tutorial you will learn about the Dart Methods and its application with practical example.

Dart Methods

A method is a set of statements that means to attach some behavior to class object. Method performs some action over class object, we give it a name so that it can be invoked later in the program. Method makes it easy to divide the complete program into sub-units that perform a specific task for that program, this way it enhance the modular approach and increase the code re-usability of the program. We pass information to methods as its parameter and method performs some action, after that it can either returns some value to the point it where it called from or returns nothing. Methods in a class can be either object method or class methods.

Instance Methods

Unless a method is declared static all of the methods in a class are instance methods. Instance methods are allowed to access instance variables and this.

Declaring Instance Methods

An instance method is defined by providing name of the method with list of parameters and return type if any.

Syntax:-

Calling Instance Method

Instance methods are applicable to objects, thus you required to create an instance of a class to invoke it.

Syntax:-

Class Methods

All of the methods declared with static keyword are class methods. Static method belong to class instead of class instances. A static method is only allowed to access the static variables of class and can invoke only static methods of the class.

Declaring Class Methods

A class method can be declared using static keyword followed by return type, followed by method name.

Syntax:-

Calling Class Method

Class methods can be invoked directly from the class name itself rather than creating an instance of it.

Syntax:-

In this tutorial we have learn about the Dart Methods and its application with practical example. I hope you will like this tutorial.