{"id":3908,"date":"2022-05-29T18:57:36","date_gmt":"2022-05-29T18:57:36","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3908"},"modified":"2022-05-29T18:57:36","modified_gmt":"2022-05-29T18:57:36","slug":"dart-static-keyword","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/29\/dart-static-keyword\/","title":{"rendered":"Dart static Keyword"},"content":{"rendered":"\n<p>The static keyword is used to declare the class variable and method. It generally manages the memory for the global data variable. The static variables and methods are the member of the class instead of an individual instance. The static variable or methods are the same for every instance of the class, so if we declare the data member as static then we can access it without creating an object. The class object is not required to access the static method or variable we can access it by putting the class name before the static variable or method. Using the class name, we can call the class method from the other classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dart Static Variable<\/h2>\n\n\n\n<p>A variable which is declared using the static keyword inside the class is called\u00a0Dart\u00a0static keyword. These are the member of the class instead of a specific instance. The static variables are treated the same for all instances of the class; it means a single copy of the static variable is shared among all instances of classes. It allocates memory once and at the class loading and uses throughout the program.<\/p>\n\n\n\n<p><strong>Point to Remember &#8211;<\/strong><\/p>\n\n\n\n<ul><li>The static variable is also identified as a class variable.<\/li><li>Single copy of the static variable is shared among the instance of a class.<\/li><li>It can be accessed using the class name. We don&#8217;t need to create an object of that class they belong to.<\/li><li>The static variables can be accessed directly in the static methods.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Declaring Static Variable<\/h2>\n\n\n\n<p>Dart provides the static keyword to declare the static variable. It is declared by using the static keyword followed by the variable name. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-static-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-static-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-static-keyword#\"><\/a><\/p>\n\n\n\n<ol><li><strong>static<\/strong>&nbsp;[data_type]&nbsp;[variable_name];&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing Static Variable<\/h2>\n\n\n\n<p>We can access the static variable by using the class name itself instead of creating an object of it. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-static-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-static-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-static-keyword#\"><\/a><\/p>\n\n\n\n<ol><li>ClassName.staticVariableName;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Dart Static Method<\/h2>\n\n\n\n<p>The concept of the static method is also similar to static variable. The static methods are the member of the class instead of the class instance. The static methods can use only static variables and can invoke the static method of the class. We don&#8217;t need to create an instance of class the access it. The static method is useful when we want to use it in other classes.<\/p>\n\n\n\n<p><strong>Points to Remember<\/strong><\/p>\n\n\n\n<ul><li>The static methods are the member class instead of its object.<\/li><li>Static methods are also identifies as class methods.<\/li><li>We can access static methods using the class name.<\/li><li>A particular copy of the static method is distributed among all the instances of a class.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Declaring Static Methods<\/h2>\n\n\n\n<p>We can declare the static method by using the static keyword followed by the method name with the return type. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-static-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-static-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-static-keyword#\"><\/a><\/p>\n\n\n\n<ol><li><strong>static<\/strong>&nbsp;return_type&nbsp;method_name()&nbsp;{&nbsp;&nbsp;<\/li><li class=\"\">&nbsp;\/\/statement(s)&nbsp;&nbsp;<\/li><li>}&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Calling Static Method<\/h2>\n\n\n\n<p>The static methods can be called by using the class name, which they belong to instead of creating an object.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-static-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-static-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-static-keyword#\"><\/a><\/p>\n\n\n\n<ol><li>className.staticMethod();&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student {  \n   static String stdBranch;  \/\/ Declaring static variable  \n   String stdName;  \n   int roll_num;  \n     \n   showStdInfo() {  \n     print(\"Student's name is: ${empName}\");  \n     print(\"Student's salary is: ${roll_num}\");  \n     print(\"Student's branch name is: ${stdBranch}\");  \n  \n      }  \n}  \n  \nvoid main() {  \n   \n  Student std1 = new Student();  \/\/ Creating instances of student class   \n  Student std2 = new Student();  \n  \/\/ Assigning value of static variable using class name   \n  Student.stdBranch = \"Computer Science\";  \n    \n  std1.stdName = \"Ben Cutting\";  \n  std1.roll_num = 90013  \n  std1.showStdInfo();  \n  \n  std2.stdName = \"Peter Handscomb\";  \n  std2.roll_num = 90014  \n  std2.showStdInfo();  \n}  <\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Student's name is: Ben Cutting\nStudent's salary is: 90013\nStudent's branch name is: Computer Science\nStudent's name is: Peter Handscomb\nStudent's salary is: 90014\nStudent's branch name is: Computer Science\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>In the above code, we declared the class called&nbsp;<strong>Student<\/strong>, which has three fields including static variable&nbsp;<strong>stdBranch<\/strong>&nbsp;and one method&nbsp;<strong>showStdInfo()<\/strong>. We created two instances of class Student and assigned values to the class variables.<\/p>\n\n\n\n<p>The static variable&nbsp;<strong>stdBranch<\/strong>&nbsp;accessed by using the class name and assigned value. Then, we called the showStdInfo() function by objects std1 and stu2. It printed details of the student as an output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The static keyword is used to declare the class variable and method. It generally manages the memory for the global data variable. The static variables and methods are the member of the class instead of an individual instance. The static variable or methods are the same for every instance of the class, so if we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[266],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3908"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=3908"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3908\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=3908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=3908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=3908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}