Constructors in Dart

Jay Tillu
Blogs by Jay Tillu
Published in
4 min readFeb 23, 2019
Constructors in Dart by Jay Tillu
  • A constructor is a special method (function) of a class that helps to create an object. As its name indicates it constructs the new object.
  • It helps to create the object by initializing values to instance variables. So we can say that its main goal is to provide values to instance variables.

How constructor is different from other methods of Class

There are some major differences between the normal method and the constructor.

  1. The constructor has the same name as the class.
  2. The constructor doesn’t have a return type.
  3. A constructor is automatically called when the object is created.
  4. If we don’t specify a constructor, the default no-argument constructor will be created.

Syntax of Declaring Constructor

class_name (arguments) {  //If there is a block of code.
// Constructor body
}

Sample Code

class Student{
Student(int enNum){
print(enNum);
}
}

main(){
var myStudent = new Student(15);
}

Output
15

Named Constructor

  • After seeing the advantages of the constructor you might want to create multiple constructors. But as we know constructor holds the name of the class. So, in that case, you might ask how can we create multiple constructors and treat them differently. Then here comes a Named Constructor in the picture.
  • By using a named constructor you can create multiple constructors in the same class. Each constructor will have a unique name. So that you can identify each of them.

Syntax of defining a named constructor

class_name.constructor_name (arguments){ }

Sample Code

class Employee {
int empID;
String empName;
String empDept;

Employee.ID(this.empID); // Named Constructor Creation

Employee.name(this.empName);

Employee.department(this.empDept);
}

main() {
var myEmployee01 = new Employee.ID(15);
var myEmployee02 = new Employee.department("Testing");
var myEmployee03 = new Employee.name("Ashu");

print(myEmployee01.empID);
print(myEmployee02.empDept);
print(myEmployee03.empName);
}

Output
15
Testing
Ashu

Conclusion

  1. Name and Purpose: Constructors share the same name as the class and are primarily responsible for creating and initializing objects. They set the initial state of an object by assigning values to its instance variables.
  2. Return Type: Unlike regular methods, constructors do not have a return type. Their main purpose is to create and configure objects, not to produce a specific result.
  3. Automatic Invocation: Constructors are automatically called when an object is instantiated. This ensures that the object is properly initialized before it can be used.
  4. Default Constructor: If a class does not explicitly define any constructors, a default no-argument constructor is automatically generated. This default constructor initializes instance variables to default values or null, depending on the programming language.
  5. Named Constructors: To provide greater flexibility and customization in object creation, named constructors can be used. Named constructors allow the creation of multiple constructors within the same class, each with a unique name. This enables different ways to initialize objects based on specific requirements or input parameters.

In summary, constructors are special methods in a class that serves as object builders and initializers. They adhere to specific naming conventions, lack return types, and are automatically invoked during object creation. The ability to create named constructors further enhances their versatility, enabling developers to define multiple construction strategies within a single class. This flexibility empowers programmers to adapt object creation to various scenarios, enhancing the overall robustness and usability of their code.

So, guys, that’s it for constructors in a dart. Again this is also the core concept of Object Oriented Programming. So practice it a little bit. Please feel free to share with me if I miss something. Until then Keep Loving, Keep Coding. And I surely catch you up in the next article.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitant that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

Learn More about Dart and Flutter

Follow me for more such content.

--

--

Jay Tillu
Blogs by Jay Tillu

I am Frontend Web Developer and Hobbyist Blogger. As a Web Developer, I hold expertise in HTML, CSS, JavaScript, and React.