Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Dart const Keyword
2.1.
Implementation of const Keyword
3.
Dart final Keyword
3.1.
Implementation of Final Keyword
4.
Frequently Asked Questions
4.1.
How does the variable declare using the final keyword determines the type of variable it will store?
4.2.
What is the state of the object that is declared as const?
4.3.
Is a new const object created every time an expression involving const is evaluated?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dart const and final keyword

Author Vasu Bansal
1 upvote

Introduction

In this blog, we will discuss the const and final keywords in Dart. They are used to assign a constant value to a variable. Sometimes, the situation requires creating variables of immutable nature, i.e. once the value of a variable is set, the program should not alter its value throughout the codebase. For example, once the value of PI is fixed in mathematics, its value should remain a constant value.

Dart const Keyword

The const keyword is used to make variables immutable. We can make the variables compile-time constant with the help of the const keyword. 

Implementation of const Keyword

We can use the const keyword as follows.

Syntax:

// This syntax can be used when the data type is not known
const variable_name

Code:

void main(){
  const ninja_batch = "Computer Science";

  print("Ninja batch: " + ninja_batch);

  // ninja_batch = "Electrical";
  // If we uncomment the above line, it would throw an error
}

Output:

Ninja batch: Computer Science

Dart final Keyword

The final keyword is used to create objects of immutable nature. The key difference between the final and const keyword lies in their usage. The const keyword is a compile-time constant, whereas the final keyword is a run-time constant. 

Implementation of Final Keyword

We can use the final keyword as follows.

Syntax:

// This syntax can be used when the data type is not known
final variable_name


// This syntax can be used when the data type is known
final [data_type] variable_name

Code:

// Dart program to demonstrate final keyword

void main(){
  final String ninja_name = "Mr X"; // a variable declared as final
  print("Ninja name: " + ninja_name);

  // ninja_name = "Mr John X";
  // If we uncomment the above statement, this will throw an error
  // As the value of final variables cannot be changed

  final int total_marks = get_total_marks(); // Getting the value of a final variable at runtime

  print("Total marks obtained by the ninja: ${total_marks}");
}

// Demo function, just return the sum of marks in three subjects
int get_total_marks(){
  int PHYSICS = 50;
  int CHEMISTRY = 70;
  int MATHS = 90;
  return PHYSICS + CHEMISTRY + MATHS;
}

Output:

Ninja name: Mr X
Total marks obtained by the ninja: 210

 

Read about Batch Operating System here.

Frequently Asked Questions

How does the variable declare using the final keyword determines the type of variable it will store?

The variable declared using the final keyword determines the type of variable it will store with the help of Dart Analyzer.

 

What is the state of the object that is declared as const?

The state of an object that is declared with the keyword const is considered to be frozen and immutable.

 

Is a new const object created every time an expression involving const is evaluated?

No, the same const object is re-used every time an expression involving const is evaluated.

Conclusion

In this article, we have extensively discussed const and final keywords and sample programs having their implementation in Dart. Feel free to refer to the blog Dart Constructors and Super Constructors to learn about this topic.

We hope this blog has helped you enhance your knowledge regarding Dart const and Final keywordsIf you want to learn more, check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass