{"id":170,"date":"2018-01-30T16:27:00","date_gmt":"2018-01-30T16:27:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2018\/01\/30\/the-static-keyword-in-c-a-simple-explanation\/"},"modified":"2018-01-30T16:27:00","modified_gmt":"2018-01-30T16:27:00","slug":"the-static-keyword-in-c-a-simple-explanation","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/the-static-keyword-in-c-a-simple-explanation\/","title":{"rendered":"The Static Keyword in  C# &#8211; A Simple Explanation"},"content":{"rendered":"<div style=\"color: #555555; font-size: 18px; line-height: 30px; text-align: justify;\">\n<div style=\"font-family: 'segoe ui';\">In this lesson, we would examine the following sub-topics:<\/p>\n<ol>\n<li><b>What is the Static Keyword<\/b><\/li>\n<li><b>When is it Necessary to use the Static Keyword<\/b><\/li>\n<li><b>Example of Static Method<\/b><\/li>\n<li><b>What of Static Classes<\/b><\/li>\n<li><b>Static Constructor <\/b><\/li>\n<li><b>Summary<\/b><\/li>\n<\/ol>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/2.bp.blogspot.com\/-PQ5vD8ASm6Y\/WnCcxtw57qI\/AAAAAAAAA7E\/m-A-ryRWxwcqJEDd5Q-JM63UoMCTCw8-ACLcBGAs\/s1600\/Static%2BKeywork%2Bin%2BC%2523-%2BA%2Bsimple%2Bexplanation.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"741\" data-original-width=\"1500\" height=\"158\" src=\"https:\/\/2.bp.blogspot.com\/-PQ5vD8ASm6Y\/WnCcxtw57qI\/AAAAAAAAA7E\/m-A-ryRWxwcqJEDd5Q-JM63UoMCTCw8-ACLcBGAs\/s320\/Static%2BKeywork%2Bin%2BC%2523-%2BA%2Bsimple%2Bexplanation.jpg\" width=\"320\" \/><\/a><\/div>\n<p><b>What is the Static Keyword<\/b><br \/>The Static keyword is used to indicate that a member variable belongs to the class itself. When applied to a method, then the method belongs to the class and not to instances of the class. The same applies to&nbsp; properties of a class. Variables or method declared as static are referred to as class variable.<br \/>Assuming a class is created with static methods, then we don&#8217;t have to create an instance of the class to use the static method.<\/p>\n<p><b>When is it Necessary to use Static keyword?<\/b><br \/>&nbsp;Static keyword is used when it is not necessary to create multiple instances of the variable. This means that a single variable exists and can be accessed across multiple instances of the class.<\/p>\n<p><b>Examples of Static Method<\/b><br \/>Consider the program below:<\/p>\n<p><span style=\"color: #0b5394;\">&nbsp;&nbsp;&nbsp; class MyGame<br \/>&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private int numberOfPlayers;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static&nbsp; String Status;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void GetStatus()<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Status = &#8220;Game Started&#8221;;<\/span><br \/><span style=\"color: #0b5394;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Status;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp; }<\/span><\/p>\n<p>If we have the code above then in the main program, we can just use the GetStatus() method without having to create an instance of the MyGame class.<br \/>So in the&nbsp; main program, we could have something like this:<\/p>\n<p><span style=\"color: blue;\"><span style=\"background-color: white;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static void Main(string[] args)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String status = MyGame.GetStatus();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp; <\/span><\/span><br \/><span style=\"background-color: blue;\"><span style=\"color: #134f5c;\"><\/span><\/span><\/p>\n<p>Here we have called the GetStatus() method directly. This is possible because the GetStatus() method is has been declared as static.<\/p>\n<p><b>What of Static Classes<\/b><br \/>A class can also be declared as static. If a class is declared as static, then that class must have only static variables.<br \/>If in the above program, we mark the class as static and try to compile, it would generate a compile time error.<\/p>\n<p><span style=\"color: #0b5394;\">&nbsp;&nbsp;&nbsp; static class MyGame<br \/>&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private int numberOfPlayers;<span style=\"color: red;\"> \/\/there would be error here in this line<\/span><br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static&nbsp; String Status;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void GetStatus()<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Status = &#8220;Game Started&#8221;;<\/span><br \/><span style=\"color: #0b5394;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Status;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp; }<\/span><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><span style=\"color: #0b5394;\"><span style=\"color: black;\">The error&nbsp; is because all the member of a static class should also be declared as static.<\/span><\/span><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b><span style=\"color: #0b5394;\"><span style=\"color: black;\">Static Constructors<\/span><\/span><\/b><\/div>\n<div style=\"font-family: 'segoe ui';\"><span style=\"color: #0b5394;\"><span style=\"color: black;\">A static constructor is use to initialized the static data members of a class. So note that the code below is valid<\/span><\/span><\/div>\n<div style=\"font-family: 'segoe ui';\"><span style=\"color: #0b5394;\"><span style=\"color: black;\">&nbsp;&nbsp; class MyGame<br \/>&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private static int numberOfPlayers;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static&nbsp; String Status;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static MyGame()<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; numberOfPlayers = 4;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Status = &#8220;Idle&#8221;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static String&nbsp; GetStatus()<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Status = &#8220;Game Started&#8221;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Status;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp; }<\/span><\/span><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><span style=\"color: black;\">In this case, the static constructor only initialized the static member variables. Also note that this constructor cannot have access modifier of public or private. This is because a static constructor is already public by default.<\/span><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><span style=\"font-family: &quot;Trebuchet MS&quot;, sans-serif;\"><b><span style=\"color: black;\">Summary<\/span><\/b><\/span><\/div>\n<ul><span style=\"font-family: &quot;Trebuchet MS&quot;, sans-serif;\"><\/span><\/p>\n<li><span style=\"font-family: &quot;Trebuchet MS&quot;, sans-serif;\"><span style=\"color: black;\">&nbsp;Static keyword indicates that a method or a variable belongs to a class<\/span><\/span><\/li>\n<p><span style=\"font-family: &quot;Trebuchet MS&quot;, sans-serif;\"><\/span><\/p>\n<li><span style=\"font-family: &quot;Trebuchet MS&quot;, sans-serif;\"><span style=\"color: black;\">A class can also be marked as static.<\/span><\/span><\/li>\n<p><span style=\"font-family: &quot;Trebuchet MS&quot;, sans-serif;\"><\/span><\/p>\n<li><span style=\"font-family: &quot;Trebuchet MS&quot;, sans-serif;\"><span style=\"color: black;\">A static method does not need an instance of the class to be called<\/span><\/span><\/li>\n<p><span style=\"font-family: &quot;Trebuchet MS&quot;, sans-serif;\"><\/span><\/p>\n<li><span style=\"font-family: &quot;Trebuchet MS&quot;, sans-serif;\"><span style=\"color: black;\">If a class is marked as static, then, all the variables and methods should also be marked as static.<\/span><\/span><\/li>\n<p><span style=\"font-family: &quot;Trebuchet MS&quot;, sans-serif;\"><\/span><\/p>\n<li><span style=\"font-family: &quot;Trebuchet MS&quot;, sans-serif;\"><span style=\"color: black;\">You can have a static constructor in a class which would be used to initialize the static members of the class<\/span><\/span><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we would examine the following sub-topics: What is the Static Keyword When is it Necessary to use the Static Keyword Example of &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[363],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/170"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":0,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}