Dart Methods

A Dart method is the collection of statements that consists of some characteristics to class object. It provides the facility to perform some operation and it can be invoked by using its name when we need in the program.

Methods in Dart

  • Methods divide the large task into small chunks and perform the specific operation of that program.

  • The methods can be defined with parameters that are passed as information to complete the specific task, after that it can return value to where it called or return nothing.

  • Methods that define in the class either instance method or class methods.

Instance Methods

A method that can be accessed by using the instance of class is called instance methods. The instance methods can be no arguments or with arguments. The instance method can access by instance variable this keyword.

Creating Instance Methods

An instance method is defined by a name and valid return type. It may have list of parameters. The syntax is given below.

Syntax:

return_type method_name(<list of arguments>)   
{  
//statement(s)  
}  

Calling instance Method

Instance methods can be accessed by using the class object, thus we required to create object to use it. The syntax is given below.

Syntax:

ClassName objName = new ClassName()  
objName.methodName() 

Class Methods

The class method is declared with the static keyword. It can be accessed by using the class name instead of the class object. These methods are common to all instances of that individual class. The static methods only can access the static variables.

Creating Class Methods

A static method is declared by using static keyword followed by method name with return type. The syntax is given below.

Syntax:

static return_type method_name(){  
  //statement(s)  
}  

Calling Class Method

The class method can be called directly by using the class name instead of class object. The syntax is given below.

Syntax:

ClassName.classMethod()