{"id":32587,"date":"2024-11-01T09:10:09","date_gmt":"2024-11-01T09:10:09","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32587"},"modified":"2024-11-01T11:54:42","modified_gmt":"2024-11-01T11:54:42","slug":"flutter-course-5-3-size-of-variables-and-instances","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32587\/","title":{"rendered":"Flutter Course: 5.3 Size of Variables and Instances"},"content":{"rendered":"<p><body><\/p>\n<p>Flutter is an open-source UI software development kit (SDK) developed by Google that allows you to easily create applications for various platforms such as Android, iOS, and the web. In section 5.3 of this tutorial, we will explore how to declare variables in Flutter and understand the size of instances. Since this topic is fundamental and core to programming, let&#8217;s go through it step by step.<\/p>\n<h2>1. What is a variable?<\/h2>\n<p>A variable is a named space that can store data. In programming, variables are used to manage and manipulate data. There are several ways to declare variables in Flutter, mostly defined using the keywords <code>var<\/code>, <code>final<\/code>, and <code>const<\/code>.<\/p>\n<h3>1.1 var<\/h3>\n<p><code>var<\/code> allows the type to be inferred automatically when declaring a variable, creating a variable whose value can be changed. For example:<\/p>\n<pre><code class=\"language-dart\">void main() {\n    var name = 'Flutter';\n    print(name); \/\/ Output: Flutter\n    name = 'Dart'; \/\/ Can be changed\n    print(name); \/\/ Output: Dart\n}<\/code><\/pre>\n<h3>1.2 final<\/h3>\n<p>The <code>final<\/code> keyword defines a variable that can be set only once. In other words, it cannot be changed after initialization. This helps enhance the safety of the program.<\/p>\n<pre><code class=\"language-dart\">void main() {\n    final int age = 10;\n    print(age); \/\/ Output: 10\n    \/\/ age = 20; \/\/ Error: final variables cannot be changed.\n}<\/code><\/pre>\n<h3>1.3 const<\/h3>\n<p><code>const<\/code> declares a compile-time constant. This means the value is determined before the program runs and cannot be changed. <code>const<\/code> is mainly used to define constant values or constant lists.<\/p>\n<pre><code class=\"language-dart\">void main() {\n    const double pi = 3.14;\n    print(pi); \/\/ Output: 3.14\n    \/\/ pi = 3.14159; \/\/ Error: const variables cannot be changed.\n}<\/code><\/pre>\n<h2>2. What is the size of an instance?<\/h2>\n<p>The size of an instance refers to the amount of memory space an object occupies. This is an important factor in optimizing memory usage efficiency in applications with a lot of dynamic data structures.<\/p>\n<p>In Flutter, it is very common to create and manage instances of objects. Accordingly, each instance needs to know how much memory its class&#8217;s properties and methods occupy.<\/p>\n<h3>2.1 Classes and Instances<\/h3>\n<p>A class is an essential element of object-oriented programming (OOP) that serves as a template for creating objects. A class can include properties (variables) and methods (functions). An instance is a concrete implementation of such a class.<\/p>\n<pre><code class=\"language-dart\">class Person {\n    String name;\n    int age;\n\n    Person(this.name, this.age);\n}\n\nvoid main() {\n    var person1 = Person('Alice', 30);\n    var person2 = Person('Bob', 25);\n    print(person1.name); \/\/ Output: Alice\n    print(person2.age); \/\/ Output: 25\n}<\/code><\/pre>\n<h3>2.2 Calculating the Size of an Instance<\/h3>\n<p>When you want to know the size of an instance in Flutter, you can use memory diagnostic tools or analyze memory usage through development tools to check the size of the instance.<\/p>\n<p>Generally, the size of an instance depends on the type and number of the class&#8217;s properties. Below are examples showing the basic size of objects in memory.<\/p>\n<ul>\n<li>String: 2 bytes (uses UTF-16) + number of characters<\/li>\n<li>int: 4 bytes<\/li>\n<li>double: 8 bytes<\/li>\n<li>bool: 1 byte<\/li>\n<\/ul>\n<p>For example, an instance of the <code>Person<\/code> class stores a name and an age, so it has the following memory structure:<\/p>\n<pre><code class=\"language-dart\">class Person {\n    String name;    \/\/ 2 bytes \u00d7 number of characters in the name\n    int age;        \/\/ 4 bytes\n}<\/code><\/pre>\n<h2>3. Tips for Optimization<\/h2>\n<p>It is important to reduce the instance size for efficient memory management. Here are some tips for optimizing instance size:<\/p>\n<h3>3.1 Removing Unnecessary Variables<\/h3>\n<p>Optimizing variables within a class can reduce memory usage and improve throughput.<\/p>\n<h3>3.2 Using Primitive Types<\/h3>\n<p>Using primitive types rather than creating new classes can help reduce instance size.<\/p>\n<h3>3.3 Lazy Initialization<\/h3>\n<p>Create instances only when needed to avoid unnecessary memory allocation. This can help reduce initial memory expenditure.<\/p>\n<pre><code class=\"language-dart\">class LazyPerson {\n    String _name;\n    int _age;\n\n    LazyPerson(String name, int age) {\n        _name = name;\n        _age = age;\n    }\n\n    \/\/ Name getter (lazy loading)\n    String get name => _name;\n\n    \/\/ Age getter (similar handling possible)\n}<\/code><\/pre>\n<h2>4. Conclusion<\/h2>\n<p>Understanding the size of variables and instances is very important in modern mobile frameworks like Flutter. By understanding the types of variables and the size of instances and managing them properly, better memory efficiency and performance can be achieved. The content covered in this tutorial is essential when developing with Flutter, so I hope you practice to gain a deeper understanding. See you in the next topic!<\/p>\n<div class=\"note\">\n<strong>Reference:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/flutter.dev\/docs\" target=\"_blank\" rel=\"noopener\">Flutter Official Documentation<\/a><\/li>\n<li><a href=\"https:\/\/dart.dev\/guide\" target=\"_blank\" rel=\"noopener\">Dart Official Guide<\/a><\/li>\n<li><a href=\"https:\/\/dart.dev\/tools\/dart-obs\" target=\"_blank\" rel=\"noopener\">Dart Observatory<\/a><\/li>\n<\/ul>\n<\/div>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flutter is an open-source UI software development kit (SDK) developed by Google that allows you to easily create applications for various platforms such as Android, iOS, and the web. In section 5.3 of this tutorial, we will explore how to declare variables in Flutter and understand the size of instances. Since this topic is fundamental &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32587\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Flutter Course: 5.3 Size of Variables and Instances&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[151],"tags":[],"class_list":["post-32587","post","type-post","status-publish","format-standard","hentry","category-flutter-course"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Flutter Course: 5.3 Size of Variables and Instances - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/32587\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Flutter Course: 5.3 Size of Variables and Instances - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Flutter is an open-source UI software development kit (SDK) developed by Google that allows you to easily create applications for various platforms such as Android, iOS, and the web. In section 5.3 of this tutorial, we will explore how to declare variables in Flutter and understand the size of instances. Since this topic is fundamental &hellip; \ub354 \ubcf4\uae30 &quot;Flutter Course: 5.3 Size of Variables and Instances&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32587\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:10:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:54:42+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32587\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32587\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Flutter Course: 5.3 Size of Variables and Instances\",\"datePublished\":\"2024-11-01T09:10:09+00:00\",\"dateModified\":\"2024-11-01T11:54:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32587\/\"},\"wordCount\":551,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Flutter course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32587\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32587\/\",\"name\":\"Flutter Course: 5.3 Size of Variables and Instances - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:10:09+00:00\",\"dateModified\":\"2024-11-01T11:54:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32587\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32587\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32587\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Flutter Course: 5.3 Size of Variables and Instances\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Flutter Course: 5.3 Size of Variables and Instances - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/32587\/","og_locale":"ko_KR","og_type":"article","og_title":"Flutter Course: 5.3 Size of Variables and Instances - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Flutter is an open-source UI software development kit (SDK) developed by Google that allows you to easily create applications for various platforms such as Android, iOS, and the web. In section 5.3 of this tutorial, we will explore how to declare variables in Flutter and understand the size of instances. Since this topic is fundamental &hellip; \ub354 \ubcf4\uae30 \"Flutter Course: 5.3 Size of Variables and Instances\"","og_url":"https:\/\/atmokpo.com\/w\/32587\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:10:09+00:00","article_modified_time":"2024-11-01T11:54:42+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32587\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32587\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Flutter Course: 5.3 Size of Variables and Instances","datePublished":"2024-11-01T09:10:09+00:00","dateModified":"2024-11-01T11:54:42+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32587\/"},"wordCount":551,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Flutter course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32587\/","url":"https:\/\/atmokpo.com\/w\/32587\/","name":"Flutter Course: 5.3 Size of Variables and Instances - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:10:09+00:00","dateModified":"2024-11-01T11:54:42+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32587\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32587\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32587\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Flutter Course: 5.3 Size of Variables and Instances"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=32587"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32587\/revisions"}],"predecessor-version":[{"id":32588,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32587\/revisions\/32588"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}