Site icon C1CTech

Java static Keyword(With Examples)

&NewLine;<p>This post is about Java static keyword along with static methods&comma; static variables&comma; static class&comma; and static blocks with the help of simple examples&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Java static Keyword<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>In Java&comma; If we want to access class members without creating an instance of the class&comma; we need to declare the class member with <span style&equals;"color&colon; &num;008000&semi;"><strong>static<&sol;strong><&sol;span> keyword&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>In Java&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>static<&sol;strong><&sol;span> keyword can be used for the following&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Blocks<&sol;strong><&sol;span><&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Variables<&sol;strong><&sol;span><&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Methods<&sol;strong><&sol;span><&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Nested classes<&sol;strong><&sol;span><&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;000000&semi;"><strong>The keyword&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><em>static<&sol;em><&sol;span>&nbsp&semi;indicates that the particular member belongs to a class itself&comma; rather than to an instance of that class<&sol;strong>&period;<&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>To create a static member &lpar;block&comma; variable&comma; method&comma; nested class&rpar;&comma; precede its declaration with the keyword&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>static<&sol;strong><&sol;span>&period;<&sol;li><li>Static members &lpar;method&comma; variables&comma; nested classes&rpar; can be accessed directly from other classes using the class name&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>class <&sol;strong>Test &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> <em>&sol;&sol; static method&NewLine; <&sol;em><&sol;span>static void <&sol;strong>m1&lpar;&rpar; &lbrace;&NewLine; System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>"from m1"<&sol;strong>&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol; non-static method&NewLine; <&sol;em><&sol;span>void <&sol;strong>m2&lpar;&rpar; &lbrace;&NewLine; System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>"from m2"<&sol;strong>&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong>class <&sol;strong>Main &lbrace;&NewLine; <strong>public static void <&sol;strong>main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; Test test &equals; <strong>new <&sol;strong>Test&lpar;&rpar;&semi;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> <em>&sol;&sol;to call non-static method we need object&NewLine; <&sol;em><&sol;span><&sol;strong>test&period;m2&lpar;&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol;calling static method using object&NewLine; <&sol;em><&sol;span><&sol;strong>test&period;<em>m1<&sol;em>&lpar;&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> <em>&sol;&sol; calling static method using class name Test&NewLine; <&sol;em><&sol;span><&sol;strong>Test&period;<em>m1<&sol;em>&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">from m2<br>from m1<br>from m1<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Static Variables<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>If a variable is&nbsp&semi;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>static<&sol;strong><&sol;span>&comma; it gets memory only once in the class area at the time of class loading and all objects of the class access the same variable&period;<&sol;li><li>Static variables are also known as <strong><span style&equals;"color&colon; &num;008000&semi;">Class variables<&sol;span><&sol;strong> because they belongs to the class rather than the object of a class&period;<&sol;li><li>Static variables can be created at class-level only&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>class <&sol;strong>Employee &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;non-static variables<&sol;span><&sol;strong>&NewLine; <strong>int eid<&sol;strong>&semi;&NewLine; <strong>int salary<&sol;strong>&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol;static variable&NewLine; <&sol;em><&sol;span>static <&sol;strong>String <em>companyCeo<&sol;em>&semi;&NewLine;&NewLine; <strong>public void <&sol;strong>show&lpar;&rpar; &lbrace;&NewLine; System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>eid <&sol;strong>&plus; <strong>" &colon; " <&sol;strong>&plus; <strong>salary <&sol;strong>&plus; <strong>" &colon; " <&sol;strong>&plus; <em>companyCeo<&sol;em>&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;public <strong>class <&sol;strong>Main &lbrace;&NewLine;&NewLine; <strong>public static void <&sol;strong>main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; Employee e1 &equals; <strong>new <&sol;strong>Employee&lpar;&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol;for non-static variable&comma; object is compulsory&NewLine; <&sol;em><&sol;span><&sol;strong>e1&period;<strong>eid <&sol;strong>&equals; 100&semi;&NewLine; e1&period;<strong>salary <&sol;strong>&equals; 5000&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol;for static variable&comma; we do not need object&NewLine; <&sol;em><&sol;span><&sol;strong>Employee&period;<em>companyCeo <&sol;em>&equals; <strong>"Arun"<&sol;strong>&semi;&NewLine;&NewLine; Employee e2 &equals; <strong>new <&sol;strong>Employee&lpar;&rpar;&semi;&NewLine; e2&period;<strong>eid <&sol;strong>&equals; 101&semi;&NewLine; e2&period;<strong>salary <&sol;strong>&equals; 8000&semi;&NewLine;&NewLine; e1&period;show&lpar;&rpar;&semi;&NewLine; e2&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">100 &colon; 5000 &colon; Arun<br>101 &colon; 8000 &colon; Arun<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><strong>Employee<&sol;strong> class contains two non-static variables <strong>eid<&sol;strong> and <strong>salary<&sol;strong> &lpar;get memory during object creation&rpar; and a static variable <strong>companyCeo<&sol;strong> &lpar;get memory when class loads&rpar;&period;<&sol;li><li>The instances <strong>e1<&sol;strong> and <strong>e2<&sol;strong> of <strong>Employee<&sol;strong> class contains the separate copies of non-static variables and access the same static variable <strong>companyCeo<&sol;strong> with value <strong>&&num;8220&semi;Arun&&num;8221&semi;<&sol;strong>&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Static Blocks<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>The <strong><span style&equals;"color&colon; &num;008000&semi;">static block<&sol;span><&sol;strong> is used to initialize the static variables and is executed only once when the class is loaded in memory&period;&nbsp&semi;<&sol;li><li>A class can have multiple static blocks and each static block is executed in the same sequence in which they have been written in a program&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>class <&sol;strong>Employee &lbrace;<br> <strong>int <&sol;strong><strong>eid<&sol;strong>&semi;<br> <strong>int <&sol;strong><strong>salary<&sol;strong>&semi;<br><br> <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;static variable<&sol;span><&sol;strong><br><&sol;em> <strong>static <&sol;strong>String <em>companyCeo<&sol;em>&semi;<br><br> <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol;execute when class loads<br><&sol;em><&sol;span><&sol;strong><em><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;first static block<&sol;span><&sol;strong><br><&sol;em> <strong>static <&sol;strong>&lbrace;<br> <em>companyCeo <&sol;em>&equals; <strong>"Arun"<&sol;strong>&semi;<br> System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>"Inside static block1"<&sol;strong>&rpar;&semi;<br> &rcub;<br><br> <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;executes when you create an object<&sol;span><&sol;strong><br><&sol;em> <strong>public <&sol;strong>Employee&lpar;<strong>int <&sol;strong>eid&comma; <strong>int <&sol;strong>salary&rpar; &lbrace;<br> <strong>this<&sol;strong>&period;<strong>eid <&sol;strong>&equals; eid&semi;<br> <strong>this<&sol;strong>&period;<strong>salary <&sol;strong>&equals; salary&semi;<br> System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>"Inside constructor"<&sol;strong>&rpar;&semi;<br> &rcub;<br><br> <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;second static block execute after the first static block<&sol;span><&sol;strong><br><&sol;em> <strong>static <&sol;strong>&lbrace;<br> <em>companyCeo <&sol;em>&equals; <strong>"Vrun"<&sol;strong>&semi;<br> System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>"Inside static block2"<&sol;strong>&rpar;&semi;<br> &rcub;<br><br> <strong>public void <&sol;strong>show&lpar;&rpar; &lbrace;<br> System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>eid <&sol;strong>&plus; <strong>" &colon; " <&sol;strong>&plus; <strong>salary <&sol;strong>&plus; <strong>" &colon; " <&sol;strong>&plus; <em>companyCeo<&sol;em>&rpar;&semi;<br> &rcub;<br>&rcub;<br><br><strong>class <&sol;strong>Main &lbrace;<br> <strong>public static void <&sol;strong>main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;<br><br> Employee e1 &equals; <strong>new <&sol;strong>Employee&lpar;100&comma; 5000&rpar;&semi;<br><br> Employee e2 &equals; <strong>new <&sol;strong>Employee&lpar;101&comma; 6000&rpar;&semi;<br><br> e1&period;show&lpar;&rpar;&semi;<br> e2&period;show&lpar;&rpar;&semi;<br> &rcub;<br>&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">Inside static block1<br>Inside static block2<br>Inside constructor<br>Inside constructor<br>100 &colon; 5000 &colon; Vrun<br>101 &colon; 6000 &colon; Vrun<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<p>As soon as the Employee class is loaded&comma;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>The first static block get executed and the value of static variable <strong>companyCeo<&sol;strong> set to <span style&equals;"color&colon; &num;008000&semi;"><strong>&&num;8220&semi;Arun&&num;8221&semi;<&sol;strong><&sol;span>&period;<&sol;li><li>Then&comma; the second static block get executed and the value of static variable <strong>companyCeo<&sol;strong> overwritten to <strong><span style&equals;"color&colon; &num;008000&semi;">&&num;8220&semi;Vrun&&num;8221&semi;&period;<&sol;span><&sol;strong><&sol;li><li>Now&comma; the constructor of e1 and e2 get executed and both will access the same static variable <strong>companyCeo<&sol;strong> with value <span style&equals;"color&colon; &num;008000&semi;"><strong>&&num;8220&semi;Vrun&&num;8221&semi;<&sol;strong><&sol;span>&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Static Methods<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><span style&equals;"color&colon; &num;000000&semi;">Static methods<&sol;span> are also known as <strong><span style&equals;"color&colon; &num;008000&semi;">class methods<&sol;span> <&sol;strong>because they belongs to the class rather than the object of a class&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>Static methods can be accessed directly in static and non-static methods but <strong>we cannot access non-static members inside static method&period;<&sol;strong><&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>class <&sol;strong>Employee &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol;non-static variables&NewLine; <&sol;em><&sol;span>int eid<&sol;strong>&semi;&NewLine; <strong>int salary<&sol;strong>&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol;static variables&NewLine; <&sol;em><&sol;span>static <&sol;strong>String <em>companyCeo<&sol;em>&semi;&NewLine; <strong>static <&sol;strong>String <em>companyName<&sol;em>&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol;Employee class constructor&NewLine; <&sol;em><&sol;span>public <&sol;strong>Employee&lpar;<strong>int <&sol;strong>eid&comma; <strong>int <&sol;strong>salary&rpar; &lbrace;&NewLine; <strong>this<&sol;strong>&period;<strong>eid <&sol;strong>&equals; eid&semi;&NewLine; <strong>this<&sol;strong>&period;<strong>salary <&sol;strong>&equals; salary&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol;non-static method&NewLine; <&sol;em><&sol;span>void <&sol;strong>show&lpar;&rpar; &lbrace;&NewLine; System&period;<strong><em>out<&sol;em><&sol;strong>&period;print&lpar;<strong>eid <&sol;strong>&plus; <strong>" &colon; " <&sol;strong>&plus; <strong>salary <&sol;strong>&plus; <strong>" &colon; "<&sol;strong>&rpar;&semi;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;access static members inside non-static method<&sol;span><&sol;strong>&NewLine; display<&sol;em>&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol;static method&NewLine; <&sol;em><&sol;span>static void <&sol;strong>display&lpar;&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol;error&colon; cannot access non-static members inside static method&NewLine; <span style&equals;"color&colon; &num;000000&semi;"> &sol;&sol;System&period;out&period;print&lpar;eid &plus; " &colon; " &plus; salary &plus; " &colon; "&rpar;&semi;&NewLine; &sol;&sol;show&lpar;&rpar;&semi;&NewLine; <&sol;span><&sol;em><&sol;span><&sol;strong>System&period;<strong><em>out<&sol;em><&sol;strong>&period;print&lpar;<em>companyName <&sol;em>&plus; <strong>" &colon; " <&sol;strong>&plus; <em>companyCeo <&sol;em>&plus; <strong>"&bsol;n"<&sol;strong>&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong>class <&sol;strong>Main &lbrace;&NewLine;&NewLine; <strong>public static void <&sol;strong>main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; Employee e1 &equals; <strong>new <&sol;strong>Employee&lpar;100&comma; 5000&rpar;&semi;&NewLine;&NewLine; Employee e2 &equals; <strong>new <&sol;strong>Employee&lpar;101&comma; 6000&rpar;&semi;&NewLine;&NewLine; <strong><em><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;to access static variables and methods&comma; we do not need object&period;&NewLine; &sol;&sol;access static variables using class<&sol;span> &NewLine; <&sol;em><&sol;strong>Employee&period;<em>companyCeo <&sol;em>&equals; <strong>"Arun"<&sol;strong>&semi;&NewLine; Employee&period;<em>companyName <&sol;em>&equals; <strong>"HCL"<&sol;strong>&semi;&NewLine;&NewLine; <strong><em><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;access static method using class<&sol;span> &NewLine; <&sol;em><&sol;strong>Employee&period;<em>display<&sol;em>&lpar;&rpar;&semi;&NewLine;&NewLine; e1&period;show&lpar;&rpar;&semi;&NewLine; e2&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">HCL &colon; Arun<br>100 &colon; 5000 &colon; HCL &colon; Arun<br>101 &colon; 6000 &colon; HCL &colon; Arun<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><strong>Employee<&sol;strong> class contains a <strong>non-static<&sol;strong> method <span style&equals;"color&colon; &num;008000&semi;"><strong>show&lpar;&rpar;<&sol;strong><&sol;span> and a <strong>static<&sol;strong> method <strong><span style&equals;"color&colon; &num;008000&semi;">display&lpar;&rpar;<&sol;span><&sol;strong>&period;<&sol;li><li>Static method display&lpar;&rpar; cannot access non-static members and&nbsp&semi;<strong>show&lpar;&rpar;<&sol;strong>&nbsp&semi;is calling static method&nbsp&semi;<strong>display&lpar;&rpar;<&sol;strong>&period;<&sol;li><li>Inside&nbsp&semi;<strong>main&lpar;&rpar;<&sol;strong>&nbsp&semi;method&comma; we are calling the non-static method show&lpar;&rpar; using the class object and static members using the class name&nbsp&semi;<strong>Employee<&sol;strong>&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Static Nested classes<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>In Java&comma; we can declare a class inside another class &lpar;Outer class&rpar;&period; Such classes are known as <span style&equals;"color&colon; &num;0000ff&semi;"><strong>nested classes<&sol;strong><&sol;span>&period; Nested classes are of two types&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><span style&equals;"color&colon; &num;008000&semi;"><strong>Static Nested Classes<&sol;strong><&sol;span><&sol;li><li><span style&equals;"color&colon; &num;008000&semi;"><strong>Non-static Nested Classes &lpar;also known as Inner classes&rpar;<&sol;strong><&sol;span><&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Differences between static and non-static nested classes&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>Nested static class&nbsp&semi;can be instantiated without instantiating its outer class &lpar;i&period;e&period; using Outer class name&rpar; but to create instance of Inner class&comma; Outer class instance is necessary&period;<&sol;li><li>A static class can access only the static members of the outer class whereas Inner class can access both static and non-static members of the outer class&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol;Outer class<&sol;em><&sol;span>&NewLine;class <&sol;strong>OuterClass &lbrace;&NewLine; <strong>private static <&sol;strong>String <em>msg <&sol;em>&equals; <strong>"Hello World"<&sol;strong>&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> <em>&sol;&sol; Static nested class&NewLine; <&sol;em><&sol;span>public static class <&sol;strong>NestedStaticClass &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol; Only static members of Outer class are accessible<&sol;em><&sol;span>&NewLine; &NewLine; public void <&sol;strong>show&lpar;&rpar; &lbrace; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol; non-static method<&sol;em><&sol;span><&sol;strong> &NewLine; System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>"Message from nested static class&colon; " <&sol;strong>&plus; <em>msg<&sol;em>&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol; Non-static nested class&NewLine; <&sol;em><&sol;span>public class <&sol;strong>InnerClass &lbrace;&NewLine;&NewLine; <strong> <em><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Both static and non-static members&NewLine; &sol;&sol; of Outer class are accessible<&sol;span> &NewLine; <&sol;em>public void <&sol;strong>display&lpar;&rpar; &lbrace; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol; non-static method<&sol;em><&sol;span><&sol;strong>&NewLine; System&period;<strong><em>out<&sol;em><&sol;strong>&period;println&lpar;<strong>"Message from non-static nested class&colon; " <&sol;strong>&plus; <em>msg<&sol;em>&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<strong>class <&sol;strong>Main &lbrace;&NewLine;&NewLine; <strong>public static void <&sol;strong>main&lpar;String args&lbrack;&rsqb;&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol; Creating instance of nested Static class<&sol;em>&NewLine; <em>&sol;&sol; using Outer class name&NewLine; <&sol;em><&sol;span><&sol;strong>OuterClass&period;NestedStaticClass obj &equals; <strong>new <&sol;strong>OuterClass&period;NestedStaticClass&lpar;&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol; Calling non-static method of nested static class&NewLine; <&sol;em><&sol;span><&sol;strong>obj&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol; To create instance of non-static nested class&comma;&NewLine; &sol;&sol; outerClass instance is necessary&period;&NewLine; <&sol;em><&sol;span><&sol;strong>OuterClass outer &equals; <strong>new <&sol;strong>OuterClass&lpar;&rpar;&semi;&NewLine; OuterClass&period;InnerClass inner &equals; outer&period;<strong>new <&sol;strong>InnerClass&lpar;&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong><em>&sol;&sol; Calling non-static method of Inner class&NewLine; <&sol;em><&sol;strong><&sol;span>inner&period;display&lpar;&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol; We can also combine above steps in one&NewLine; &sol;&sol; step to create instance of Inner class&NewLine; <span style&equals;"color&colon; &num;000000&semi;">&sol;&sol;OuterClass&period;InnerClass innerObject &equals; new OuterClass&lpar;&rpar;&period;new InnerClass&lpar;&rpar;&semi;&NewLine; <&sol;span><&sol;em><&sol;span><&sol;strong>&rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">Message from nested static class&colon; Hello World&NewLine;Message from non-static nested class&colon; Hello World<&sol;pre>&NewLine;&NewLine;

Exit mobile version