2017-11-16

Java Static Keyword

Static Keyword
===============
Static Keyword mainly used for the memory management.
Static keyword can use in with,
1) Variable
2) Method
3) Blocks
4) Nested Class
The Static keyword belongs to the class than instance of the class.


1)Static Variable
  ================
=> If you declair any variable with static keyword then it is called as static variable
=> The Static keyword is use to refer the common propoties of all subjest,
   such as ,college name of the all students,company name of the all employees.
=> Static variable gets memory only once at the time of class loading.

Example of static variable:-
           
                                Class  Variable
                                {
                                static int roll_no = 2;
                                static String name = "xyz";
                                public static void main(String[] args)
                                {
                                System.out.println(roll_no);
                                System.out.println(name);
                                }
                                }

                               
2) Static Method
   ==============                               
=> If you use static keyword with any method ,that method called as a static method.
=> A static method belongs to the class rather than object of a class.
=> A static method can be invoked without the need for creating an instance of a class.
=> A static method can access static data member and can change the value of it.
=> Static method cannot use non-static data member or non-static method directly
=> This and super keywords cannot be used in static context

Example of Static Method:- 
                         1) No return type
                         ===================
                          
                         public class StaticMethod
                               {
                            static void test()
                               {
                                 int i =0;
                                 int j =10;
                            System.out.println(i);
                            System.out.println(j);
                               }

                         public static void main(String[] args)
                               {
                            StaticMethod.test();
   
                               }
                         
                                  }
                        2) with return type
                        =====================
                       
                       
                        public class StaticMethod2
                        {
                          static int test()
                        {
                          int i =0;
                          int j =10;
                          int k =i+j;
                          return k;
           
                        }
                        public static void main(String[] args)
                        {
                         int m =StaticMethod2.test();
                         System.out.println(m);
                        }
                        }



3)Static Blocks
  =============
=> Static block use to initilize thye static data members.
=> Static blocks executed before main method at the time of class loading.

                  
                  
Example of Static Blocks:-
                          1)
                             Class StaticBlock
                                 {
                                 static{
                                   System.out.println("from StaticBlock");
                                   }


                                 public static void main(String[] args)
                                     {System.out.println("from main");
                                       }                               
                          2)
                         
                           Class StaticBLock2
                           {
                             static int i =0;
                             static int j =0;
                                  static{
                                    int i =1;
                                    int j =10;
                                  System.out.println(i);
                                  System.out.println(j);
                                        }
                                  static{
                                    int i =12;
                                    int j = 13;
                                    System.out.println(i);
                                    System.out.println(j);
                                        }
                           public static void main(String[] args)
                              {
                                  System.out.println(i);
                                  System.out.println(j);}
                              }
                         

       
       
4) Nested Static Class
  ===================
=>A class can be static if it is a Nested class.
=>Nested static class doesn't need reference of outer class.
=>A static class cannot access non-static members of outer class.


Example of  Nested Static Class:-
                                 public class StaticNested {
                                      static int i =12;
                                          static class new1{
                                                          
                                       public void NewMethod()
                                                      {
                                       System.out.println(i);
                                                      }
                                                      }
                                      public static void main(String[] args) {
                                     StaticNested.new1 nw = new StaticNested.new1();
                                                   nw.NewMethod();
                                                      }
                                                      }

     
                                   

                       

                           
                           
                           
                           
                           

No comments:

Post a Comment