> For the complete documentation index, see [llms.txt](https://des-vu-technologies.gitbook.io/blogs-by-shankar/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://des-vu-technologies.gitbook.io/blogs-by-shankar/oop-with-dart/constructor/constructor.md).

# constructor

Constructors in Dart are special methods used for creating and initializing objects of a class. They are crucial for setting up the initial state of objects. Dart provides several types of constructors, each serving a specific purpose:

1. **Default Constructor**:

   * If you don't explicitly define any constructors in your class, Dart provides a default constructor with no arguments.
   * The default constructor initializes fields with their default values (e.g., `null` for objects, `0` for integers, `false` for booleans).

   ```dart
   class Person {
     String name;
     int age;
   }

   void main() {
     var person = Person();
     print('${person.name} ${person.age}'); // Output: null 0
   }
   ```
2. **Parameterized Constructor**:

   * A parameterized constructor allows you to initialize class fields by providing parameters when creating an object.

   ```dart
   class Person {
     String name;
     int age;

     // Parameterized constructor
     Person(this.name, this.age);
   }

   void main() {
     var person = Person('Alice', 30);
     print('${person.name} ${person.age}'); // Output: Alice 30
   }
   ```
3. **Named Constructor**:

   * Named constructors are used to define additional named constructors in a class.
   * They provide alternative ways to create objects with specific configurations.

   ```dart
   class Person {
     String name;
     int age;

     // Named constructor
     Person.guest() {
       name = 'Guest';
       age = 18;
     }
   }

   void main() {
     var person = Person.guest();
     print('${person.name} ${person.age}'); // Output: Guest 18
   }
   ```
4. **Factory Constructor**:

   * Factory constructors are used when you want to control the object creation process.
   * They may return an instance of the class, potentially from a cache or by reusing an existing object.

   ```dart
   class Singleton {
     static Singleton _instance;

     // Private constructor
     Singleton._();

     factory Singleton.getInstance() {
       if (_instance == null) {
         _instance = Singleton._();
       }
       return _instance;
     }
   }

   void main() {
     var instance1 = Singleton.getInstance();
     var instance2 = Singleton.getInstance();

     print(identical(instance1, instance2)); // Output: true (Both references point to the same object)
   }
   ```
5. **Initializer List Constructor**:

   * An initializer list constructor allows you to perform additional initialization of fields before the constructor's body runs.

   ```dart
   class Point {
     final int x;
     final int y;

     // Initializer list constructor
     Point(this.x, this.y) : assert(x >= 0), assert(y >= 0);
   }

   void main() {
     var point = Point(2, 3);
     print('Point: ${point.x}, ${point.y}'); // Output: Point: 2, 3
   }
   ```
6. **Constant Constructor**:

   * Constant constructors are used to create constant objects. The `const` keyword is used to invoke them.
   * Objects created with a constant constructor are guaranteed to be identical and immutable at compile-time.

   ```dart
   class Circle {
     final double radius;

     // Constant constructor
     const Circle(this.radius);

     double get area => 3.14159265359 * radius * radius;
   }

   void main() {
     const Circle circle1 = Circle(5.0);
     const Circle circle2 = Circle(5.0);

     print(circle1.area); // Output: 78.539816339745
     print(identical(circle1, circle2)); // Output: true (Both objects are the same)
   }
   ```

These are the primary types of constructors in Dart, each serving different purposes and allowing you to create and initialize objects in various ways to suit your application's needs.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://des-vu-technologies.gitbook.io/blogs-by-shankar/oop-with-dart/constructor/constructor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
