{"id":1218,"date":"2022-02-25T17:02:34","date_gmt":"2022-02-25T17:02:34","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1218"},"modified":"2022-02-25T17:02:34","modified_gmt":"2022-02-25T17:02:34","slug":"nested-static-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/25\/nested-static-class\/","title":{"rendered":"Nested Static Class"},"content":{"rendered":"\n<p>In this tutorial, you will learn about nested static class with the help of examples. You will also learn about how static classes differs from inner classes.<\/p>\n\n\n\n<p id=\"introduction\">As learned in previous tutorials, we can have a class inside another class in Java. Such classes are known as nested classes. In Java, nested classes are of two types:<\/p>\n\n\n\n<ul><li>Nested non-static class (Inner class)<\/li><li>Nested static class.<\/li><\/ul>\n\n\n\n<p>We have already discussed inner classes in the previous tutorial. Visit\u00a0Java Nested Class\u00a0if you want to learn about inner classes.<\/p>\n\n\n\n<p>In this tutorial, we will learn about nested static classes.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"nested-static\">Java Nested Static Class<\/h2>\n\n\n\n<p>We use the keyword&nbsp;<code>static<\/code>&nbsp;to make our nested class static.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;In Java, only nested classes are allowed to be static.<\/p>\n\n\n\n<p>Like regular classes, static nested classes can include both static and non-static fields and methods. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class Animal {\n   static class Mammal {\n      \/\/ static and non-static members of Mammal\n   }\n   \/\/ members of Animal\n} \n<\/code><\/pre>\n\n\n\n<p>Static nested classes are associated with the outer class.<\/p>\n\n\n\n<p>To access the static nested class, we don\u2019t need objects of the outer class.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Static Nested Class<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n\n\/\/ inner class\n   class Reptile {\n      public void displayInfo() {\n        System.out.println(\"I am a reptile.\");\n      }\n   }\n\n\/\/ static class\n   static class Mammal {\n      public void displayInfo() {\n        System.out.println(\"I am a mammal.\");\n      }\n   }\n}\n\nclass Main {\n   public static void main(String&#91;] args) {\n      \/\/ object creation of the outer class\n      Animal animal = new Animal();\n\n      \/\/ object creation of the non-static class\n      Animal.Reptile reptile = animal.new Reptile();\n      reptile.displayInfo();\n\n      \/\/ object creation of the static nested class\n      Animal.Mammal mammal = new Animal.Mammal();\n      mammal.displayInfo();\n\n   }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>I am a reptile.\nI am a mammal.\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above program, we have two nested class&nbsp;<var>Mammal<\/var>&nbsp;and&nbsp;<var>Reptile<\/var>&nbsp;inside a class&nbsp;<var>Animal<\/var>.<\/p>\n\n\n\n<p>To create an object of the non-static class Reptile, we have used<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Animal.Reptile reptile = animal.new Reptile()\n<\/code><\/pre>\n\n\n\n<p>To create an object of the static class&nbsp;<var>Mammal<\/var>, we have used<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Animal.Mammal mammal = new Animal.Mammal()\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"access-outer-class\">Accessing Members of Outer Class<\/h2>\n\n\n\n<p>In Java, static nested classes are associated with the outer class. This is why static nested classes can only access the class members (static fields and methods) of the outer class.<\/p>\n\n\n\n<p>Let\u2019s see what will happen if we try to access non-static fields and methods of the outer class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Accessing Non-static members<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n  static class Mammal {\n   public void displayInfo() {\n     System.out.println(\"I am a mammal.\");\n   }\n }\n\n class Reptile {\n   public void displayInfo() {\n     System.out.println(\"I am a reptile.\");\n   }\n }\n\n public void eat() {\n   System.out.println(\"I eat food.\");\n }\n}\n\nclass Main {\n public static void main(String&#91;] args) {\n   Animal animal = new Animal();\n   Animal.Reptile reptile = animal.new Reptile();\n   reptile.displayInfo();\n\n   Animal.Mammal mammal = new Animal.Mammal();\n   mammal.displayInfo();\n   mammal.eat();\n }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Main.java:28: error: cannot find symbol\n    mammal.eat();\n          ^\n  symbol:   method eat()\n  location: variable mammal of type Mammal\n1 error\ncompiler exit status 1\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a non-static method&nbsp;<code>eat()<\/code>&nbsp;inside the class&nbsp;<var>Animal<\/var>.<\/p>\n\n\n\n<p>Now, if we try to access&nbsp;<code>eat()<\/code>&nbsp;using the object&nbsp;<var>mammal<\/var>, the compiler shows an error.<\/p>\n\n\n\n<p>It is because&nbsp;<var>mammal<\/var>&nbsp;is an object of a static class and we cannot access non-static methods from static classes.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"top-level\">Static Top-level Class<\/h2>\n\n\n\n<p>As mentioned above, only nested classes can be static. We cannot have static top-level classes.<\/p>\n\n\n\n<p>Let\u2019s see what will happen if we try to make a top-level class static.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static class Animal {\n public static void displayInfo() {\n   System.out.println(\"I am an animal\");\n }\n}\n\nclass Main {\n public static void main(String&#91;] args) {\n   Animal.displayInfo();\n }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Main.java:1: error: modifier static not allowed here\nstatic class Animal {\n       ^\n1 error\ncompiler exit status 1\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have tried to create a static class&nbsp;<var>Animal<\/var>. Since Java doesn\u2019t allow static top-level class, we will get an error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about nested static class with the help of examples. You will also learn about how static classes differs from inner classes. As learned in previous tutorials, we can have a class inside another class in Java. Such classes are known as nested classes. In Java, nested classes are of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[556],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1218"}],"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=1218"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1218\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}