What is the difference between the “const” and “final” keywords in Dart?

前端 未结 11 1331
温柔的废话
温柔的废话 2020-11-27 10:52

What is the difference between const and final keyword in Dart?

11条回答
  •  生来不讨喜
    2020-11-27 11:22

    Consolidated @Meyi and @faisal-naseer answers and Comparing with little programming.

    const:

    const keyword used to make a variable to store a compile time constant value. Compile time constant value is a value which will be constant while compiling :-)

    For example 5 is a compile time constant. While DateTime.now() which is not compile time constant. Because this method will return the time when the line is getting executed at runtime. So we can't assign the DateTime.now() to a const variable.

    const a = 5;
    // Uncommenting below statement will cause compile time error.
    // Because we can't able to assign a runtime value to a const variable
    // const b = DateTime.now();
    

    Should be initialized at the same line.

    const a = 5;
    // Uncommenting below 2 statement will cause compilation error.
    // Because const variable must be initialized at the same line.
    // const b;
    // b = 6;
    

    All statements mentioned below are acceptable.

    // Without type or var
    const a = 5;
    // With a type
    const int b = 5;
    // With var
    const var c = 6;
    

    Class level const variable should be initialized like below.

    Class A {
        static const a = 5;
    }
    

    Instance level const variable is not possible.

    Class A {
        // Uncommenting below statement will give compilation error.
        // Because const is not possible to be used with instance level 
        // variable.
        // const a = 5;
    }
    

    The another major use of const is used to make the object immutable. To make a class object immutable we need to use the const keyword with constructor and make all the fields as final like mentioned below.

    Class A {
        final a, b;
        const A(this.a, this.b);
    }
    
    void main () {
        // There is no way to change a field of object once it's 
        // initialized.
        const immutableObja = const A(5, 6);
        // Uncommenting below statement will give compilation error.
        // Because you are trying to reinitialize a const variable
        // with other value
        // immutableObja = const A(7, 9);
    
        // But the below one is not the same. Because we are mentioning objA 
        // is a variable of a class A. Not const. So we can able to assign
        // another object of class A to objA.
        A objA = const A(8, 9);
        // Below statement is acceptable.
        objA = const A(10, 11);
    }
    

    we can use const keyword to a list.

    const a = const [] - A variable a initialized as const which contains a list of const objects(i.e., The list should contain only compile time constant and immutable objects). So we can't able to assign a with another list.

    var a = const [] - A variable a initialized as var which contains a list const objects. So we can able to assign another list to the variable a.

    Class A {
        final a, b;
        const A(this.a, this.b);
    }
    
    class B {
        B(){ // Doing something }
    }
    
    void main() {
        const constantListOfInt = const [5, 6, 7,
                     // Uncommenting below statement give compilation error.
                     // Because we are trying to add a runtime value
                     // to a constant list
                     // DateTime.now().millisecondsSinceEpoch
                  ];
        const constantListOfConstantObjA = const [
            A(5, 6),
            A(55, 88),
            A(100, 9),
        ];
        // Uncommenting below 2 statements will give compilation error.
        // Because we are trying to reinitialize with a new list.
        // constantListOfInt = [8, 9, 10];
        // constantListOfConstantObjA = const[A(55, 77)];
    
        // But the following lines are little different. Because we are just
        // trying to assign a list of constant values to a variable. Which 
        // is acceptable
        var variableWithConstantList = const [5, 6, 7];
        variableWithConstantList = const [10, 11, 15];
        var variableOfConstantListOfObjA = const [A(5, 8), A(7, 9), A(10, 4)];
        variableWithConstantList = const [A(9, 10)];
    }
    

    final:

    final keyword also used to make the variable to hold a constant value. Once initialized we can't able to change the value.

    final a = 5;
    // Uncommenting below statement will give compilation error.
    // Because a is declared as final.
    // a = 6;
    

    All statements mentioned below are acceptable.

    // Without type or var
    final a = 5;
    // With a type
    final int b = 5;
    // With var
    final var c = 6;
    

    Can able to assign a runtime value.

    // DateTime.now() will return the time when the line is getting
    // executed. Which is a runtime value.
    final a = DateTime.now();
    var b = 5;
    final c = b;
    

    Class level final variable must be initialized in the same line.

    Class A {
        static final a = 5;
        static final b = DateTime.now();
    }
    

    Instance level final variable must be initialized in the same line or in the constructor initialization. The value will be put into memory when the object is created.

    Class A {
        final a = 5;
    }
    
    // Constructor with a parameter.
    Class B {
        final b;
        B(this.b);
    }
    
    // Constructor with multiple parameter.
    Class C {
        final c;
        C(this.c, int d) {
            // Do something with d
        }
    }
    
    void main() {
        A objA = new A();
        B objB = new B(5);
        C objC = new C(5, 6);
    }
    

    Assigning a list.

    final a = [5, 6, 7, 5.6, A()];
    // Uncommenting Below statement will give compilation error.
    // Because we are trying to reinitialize the object with another list.
    // a = [9.9, 10, B()];
    

提交回复
热议问题