{"id":1182,"date":"2016-03-21T06:58:23","date_gmt":"2016-03-21T06:58:23","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=1182"},"modified":"2016-03-21T06:58:23","modified_gmt":"2016-03-21T06:58:23","slug":"static-keyword","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/static-keyword.html","title":{"rendered":"Static Keyword In Java With Examples"},"content":{"rendered":"<p>Static keyword in java is mainly used to save memory as with the help of static we can declare data one time and access it in a whole program where we need the same data more than one time in a program. After creating static keyword there is no need to declare that data again and again. Static keyword can be used with:<\/p>\n<ul>\n<li>Nested class<\/li>\n<li>Method<\/li>\n<li>Variable<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Static Nested Class<\/strong><\/h4>\n<p>A static nested class is a class that is declared inside another class with a <strong>static <\/strong>modifier. A static nested class can refer directly to static members of the enclosing classes, even if those members are private.<\/p>\n<p><span style=\"color: #008000;\"><strong>Basic Structure of Static Nested Class:<\/strong><\/span><\/p>\n<p>The basic structure of creating a static nested class is as follows:<\/p>\n<pre>class OuterClass\r\n{\r\n\r\n  static class InnerClass\r\n  {\r\n\r\n    \/\/body of static nested class\r\n\r\n  }\r\n\r\n  \/\/more members of outside class\r\n\r\n}<\/pre>\n<p>Nested classes are generally made static when they have a strong relationship with the enclosing class, but their existence is independent of an instance of the enclosing class. Unlike inner classes, you can instantiate the static nested class even if you have not instantiated the outer class. <strong>For example:<\/strong><\/p>\n<pre>OuterClassName.InnerClassName=new OuterClassName.InnerClassName();<\/pre>\n<p><span style=\"color: #008000;\"><strong>Program Example of Static Nested Class:<\/strong><\/span><\/p>\n<p>The following program demonstrates the use of static nested class.<\/p>\n<p><strong>Step 1: <\/strong>First we create a class <strong>OuterClass <\/strong>in which we create static class <strong>InnerClass:<\/strong><\/p>\n<pre>class OuterClass\r\n{\r\n\r\n  double d=16.5;\r\n\r\n  static String s=\"Static\";\r\n\r\n  static class InnerClass\r\n  {\r\n\r\n    int x;\r\n\r\n    public void show()\r\n    {\r\n\r\n       x=6;\r\n\r\n       System.out.println(\"value of x is:\"+x);\r\n\r\n       System.out.println(\"value of s is:\"+s);\r\n\r\n     }\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Step 2: <\/strong>Second we create a class <strong>StaticNested <\/strong>in which we create an object of static nested class <strong>InnerClass:<\/strong><\/p>\n<pre>class StaticNested\r\n{\r\n\r\n  public static void main(String[] args)\r\n  {\r\n\r\n    OuterClass.InnerClass obj1=new OuterClass.InnerClass();\r\n\r\n    Obj1.show();\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>value of x is: 6\r\nvalue of s is: Static<\/pre>\n<p><strong>Explanation of Example:<\/strong><\/p>\n<p>In the above example, as static keyword is used inside the innerclass so it will be called as\u00a0static class. The s variable is also declared as static so it can be called in the static class.<\/p>\n<hr \/>\n<h4><strong>Static Variable<\/strong><\/h4>\n<p>Any variable when declared with the keyword \u201cstatic\u201d, it is known as static variable or class variable. Static variable is used to fulfill the common properties or behavior of all objects. For example: institute name for\u00a0students is common for all students. The static variable gets memory at class loading time only once in class area.<\/p>\n<p><span style=\"color: #008000;\"><strong>Program Example of Static Variable:<\/strong><\/span><\/p>\n<p>Let us take an example to show the use of static variable.<\/p>\n<pre>class\u00a0Employee\r\n{\r\n\r\n  int\u00a0id;\r\n\r\n  String\u00a0name;\r\n\r\n  static\u00a0String\u00a0company\u00a0=\"Ducat\";\r\n\r\n  Employee(int\u00a0i,String\u00a0n)\r\n  {\r\n\r\n    id\u00a0=\u00a0i;\r\n\r\n    name\u00a0=\u00a0n;\r\n\r\n  }\r\n\r\n  void\u00a0print\u00a0()\r\n  {\r\n\r\n    System.out.println(id+\"\u00a0\"+name+\"\u00a0\"+company);\r\n\r\n  }\r\n\r\n  public\u00a0static\u00a0void\u00a0main(String\u00a0args[])\r\n\r\n  {\r\n\r\n    Employee\u00a0e1\u00a0=\u00a0new\u00a0Employee(111,\"Gourav\");\r\n\r\n    Employee e2\u00a0=\u00a0new\u00a0Employee(222,\"Nishant\");\r\n\r\n    e1.print();\r\n\r\n    e2.print();\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>111 Gourav Ducat\r\n222 Nishant Ducat<\/pre>\n<p><strong>Explanation of Program:<\/strong><\/p>\n<p>The above program has a class <strong>Employee<\/strong> in which we use <strong>static<\/strong> variable company. This variable is common to all employees so we take it static and used to display employee information with different id and name but same company. The advantage to make company variable static is that it save memory as it loads once in a class area at class loading time.<\/p>\n<p><span style=\"color: #008000;\"><strong>Important Points of Static Variable:<\/strong><\/span><\/p>\n<ul>\n<li>There is only one copy of the static variable in a class which is shared among all the objects of the class.<\/li>\n<li>A static variable can be accessed before any object or instance of a class is created, without making reference to any object.<\/li>\n<li>We can make a variable as both static and final by making a static variable constant in java.<\/li>\n<\/ul>\n<p>For more details read <a href=\"\/java\/static-variable-example.html\">static variable\u00a0in JAVA<\/a><\/p>\n<hr \/>\n<h4><strong>Static Method<\/strong><\/h4>\n<p>The static method is similar to instance or class method of a class but with the difference that the static method can be called through the name of class without creating any instance of that class. A static method is also called class method as it is related with a class and not with individual instance of the class.<\/p>\n<p>It is important to note that\u00a0static method can only access static block\u00a0or fields\u00a0and other static methods of a class. This is because static methods can be used even if no object of that class has been created. We will show this in the example below. In case\u00a0from the static method you try to\u00a0access a non static field then compilation error will be generated as the static method does not know which non static field value to use.<\/p>\n<p><span style=\"color: #008000;\"><strong>Program Example of Static Method:<\/strong><\/span><\/p>\n<p>Let us take an example to show the use of static method.<\/p>\n<p><strong>Step 1: <\/strong>First we create a class <strong>Methods <\/strong>in which we declare two static methods:<\/p>\n<pre>class Methods\r\n{\r\n\r\n  static int add(int a,int b)\r\n  {\r\n\r\n    return (a+b);\r\n\r\n  }\r\n\r\n  static void show()\r\n  {\r\n\r\n    System.out.println(\"show method\");\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Step 2: <\/strong>Second we create a class <strong>StaticMethod <\/strong>in which we call the above class methods:<\/p>\n<pre>class StaticMethod extends Methods\r\n{\r\n\r\n  public static void main(String[] args)\r\n  {\r\n\r\n    int result =add(6, 7);\r\n\r\n    System.out.println(\"Result is:\" + result);\r\n\r\n    show();\r\n\r\n   }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Result is: 13\r\nshow method<\/pre>\n<p><span style=\"color: #008000;\"><strong>Important Points of Static Method:<\/strong><\/span><\/p>\n<ul>\n<li>A static method should be called using the class name rather than an object reference variable.<\/li>\n<li>A static method can neither access a non static variable nor a non static method.<\/li>\n<li>If you have a class with only static methods and you do not want to create an object of class, you can mark the constructor private.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Static keyword in java is mainly used to save memory as with the help of static we can declare data one time and access it in a whole program where we need the same data more than one time in a program. After creating static keyword there is no need to declare that data again &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/static-keyword.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Static Keyword In Java With Examples<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,79],"tags":[],"class_list":["post-1182","post","type-post","status-publish","format-standard","hentry","category-archieve","category-java-basics"],"psp_head":"<title>Static Keyword In Java With Examples \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn Static keyword used in JAVA i.e. about static methods, static variable and static class with examples. Static keyword in java is mainly used to save memory as with the help of static we can declare data one time and access it in a whole program where we need the same data more than one time in a program.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/static-keyword.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=1182"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1182\/revisions"}],"predecessor-version":[{"id":1456,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1182\/revisions\/1456"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=1182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=1182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}