In this article i have explained the use of 'Static' keyword in different place of java.
like
1.Instance variable
2.In methods
3.In Class
4.In Inner Class -Create Object and call inner static and Non-Static methods.
5.Purpose of Static method and Static Class,static instance variable.
With Sample Program.
S.No | Place of static | Description | When executes | Where Not Include | Sample Code |
1 | Static block | Nothing but the block of code will be written inside of the static keyword in class . | Called only first time of object creation. First priority will always go to static block even normal block of code present | Inside Methods we can't declare a static block. | Package staticpck.block; public class StaticBlock { { System.out.println("Inside empty block"); } static {System.out.println("Inside static Block");} }
package staticpck.block; public class StaticBlockMain { public static void main(String arg[]) {StaticBlock staticBlock=new StaticBlock(); StaticBlock staticBlock1=new StaticBlock(); } }
Output: Inside static Block Inside empty block Inside empty block
|
2 | Static Instance Variable | Only one copy for the entire class.(each object reference have only one copy). Even you can access static variable with out creating reference for Object | Only one copy for entire class (Only class name is enough to refer instance variable) | NA(Inside Method local static variable are not possible) | package staticpck.Instvar;
public class InstanceVar { static int Instance=1; } package staticpck.Instvar; public class InstanceVarMain { public static void main(String arg[]) { InstanceVar instanceVar=new InstanceVar(); instanceVar.Instance=2; InstanceVar instanceVar2=new InstanceVar(); instanceVar2.Instance=3; System.out.println("instanceVar.Instance="+instanceVar.Instance); }}
Output: instanceVar.Instance=3
|
3 | Static @ method | Only one copy for the entire class.(each object reference have only one copy). Even you can access static variable with out creating reference for Object | When a method function is independent of its reference variable in that case we can use Static method. |
| package staticpck.method; public class Method { public static String getClassDesc() { return "This Class Refers Static Method";} }
package staticpck.method;
public class MethodImp { public static void main(String[] args) { System.out.println("With Out Obj Ref Call::"+Method.getClassDesc()); System.out.println("With Obj Ref Call::"+new Method().getClassDesc()); }}
OutPut: With Out Obj Ref Call::This Class Refers Static Method With Obj Ref Call::This Class Refers Static Method |
4 | Static @ class | We can't use static for class. | - | Compile time error | - |
5 | Static in inside method | We can't use static inside method. No use to keep its in Stack memory | - | Compile time error | - |
6 | Inner class : static @ instance variable | Error msg: The field “sample” cannot be declared static; static fields can only be declared in static or top level types | - | Compile time error | To eliminate error declare inner class as static |
7 | Inner class : static @ method | Error msg: The method “innermethod” cannot be declared static; static methods can only be declared in a static or top level type | - | Compile time error | To eliminate error declare inner class as static |
8 | Static @Inner class | If u want to declare static instance/static methods inside the Inner class than Inner class must be declared as Static | All execution are same for outer class |
| package staticpck.inner; public class InnerSample { static class InnerStatic{ static{ System.out.println("hi i am from inner static block");
} { System.out.println("Hi i am from Inner class block"); } static int instance; static String InnerMethod() { return "I am from Static InnerMethod"; } } class Inner{ //static int instance; String InnerMethod() { return "I am not able to use static due to My hirearchy"; } } }
package staticpck.inner;
public class InnerSampleImp {
public static void main(String arg[]) { System.out.println(InnerSample.InnerStatic.InnerMethod()); InnerSample innerSample=new InnerSample(); InnerSample.Inner inner=innerSample.new Inner(); System.out.println(); } }
Output: hi i am from inner static block Innser Static class:I am from Static InnerMethod |
No comments:
Post a Comment