
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Methods in Dart Programming
A method is a combination of statements which is used to attach some behavior to the class objects. It is used to perform some action on class objects, and we name methods so that we can recall them later in the program.
Methods help in making the core more modular and in increasing the re-usability of the program.
Information can be passed to methods through the parameters and then it can perform some operation on that information or it can even return values.
Methods in a class are of two types, these are −
Instance methods
Class methods
Instance Methods
Instance methods are methods that are present inside a class and don't make use of the static keyword while declaration. Instance methods can access instance variables and this.
Syntax
returnType methodName(){ // statements(s) }
We can invoke the instance methods by making an instance object of the class and then invoking the methods on the instance of the object.
Example
Consider the example shown below −
class Sample{ var name = "Mukul"; void printName(name){ print(name); } } void main(){ Sample smp = new Sample(); smp.printName("TutorialsPoint"); }
Output
TutorialsPoint
Class Methods
If we declare a method using the static keyword then that method is known as a class method. Static methods belong to class instead of class instances, like we saw in the above example.
A static method cannot be invoked through an instance of a class, and is allowed to access the static variables of class and can also invoke only the static methods of the class.
Syntax
static returnType methodName() { // statement(s) }
Example
Let's write an example where we invoke a class method in a dart program.
Consider the example shown below −
class Sample{ var name = "Mukul"; static void printName(name){ print(name); } } void main(){ Sample.printName("Static Method"); }
Output
Static Method