Mastering Flutter: Understanding the Differences Between Static, Final, and Const

 Sure, here are short definitions for static, final, and const:


Static: Refers to class-level attributes or methods. static members are shared across all instances of the class, meaning they belong to the class itself, not to any specific object.


Final: Used to declare variables that can be assigned only once. A final variable must be initialized when it's declared, and its value cannot be changed later. In object-oriented programming, final can also be used to indicate that a method cannot be overridden or a class cannot be inherited.


Const: Indicates that a variable's value is constant and known at compile-time. const variables are immutable, and their values are fixed for the entire runtime of the program. In languages like Dart, const also implies the variable is final



class Student {

  // Static variable

  static String? stdBranch;


  // Instance variables

  String? stdName;

  int? roll_num;


  // Method to show student information

  showStdInfo() {

    print("Student's name is: ${stdName}");

    print("Student's roll number is: ${roll_num}");

    print("Student's branch name is: ${stdBranch}");

  }

}


void main() {

  // Creating instances of the Student class

  Student std1 = Student();

  Student std2 = Student();

  Student std3 = Student();


  // Assigning value of static variable using class name

  Student.stdBranch = "Soft Solution Service";


  // Assigning values to std1

  std1.stdName = "Praveen";

  std1.roll_num = 101;

  std1.showStdInfo();


  // Assigning values to std2

  std2.stdName = "Raj Suthar";

  std2.roll_num = 102;

  std2.showStdInfo();

  

  std3.stdName = "Kamal";

  std3.roll_num = 103;

  std3.showStdInfo();

}


........................................................................ 


class ExampleClass {

  // Static variable - shared by all instances of the class

  static int staticCounter = 0;


  // Final variable - its value can be set once and only once

  final String instanceName;


  // Const variable - a compile-time constant

  static const int maxLimit = 100;


  ExampleClass(this.instanceName) {

    staticCounter++; // Increment the static counter on each new instance

  }


  static void displayStaticCounter() {

    // Static method accessing a static variable

    print('Number of instances created: $staticCounter');

  }


  void displayInstanceName() {

    // Instance method accessing a final variable

    print('Instance name: $instanceName');

  }

}


void main() {

  var example1 = ExampleClass('Praveen');

  var example2 = ExampleClass('Raj');


  ExampleClass.displayStaticCounter(); // Outputs: Number of instances created: 2

  example1.displayInstanceName(); // Outputs: Instance name: First Instance

  example2.displayInstanceName(); // Outputs: Instance name: Second Instance


  print('The maximum limit is ${ExampleClass.maxLimit}.'); // Outputs: The maximum limit is 100.

}


Comments

Popular posts from this blog

Unlocking the Power of OOP: A Beginner's Guide to Objects, Encapsulation, Inheritance, Abstraction, and Polymorphism

HTTP GET Response in Flutter

Building a Flutter Firebase Firestore CRUD App