What is the static keyword in Java

Hi Friends, In this topic we will discuss the use of static keyword in Java. Every Java Programmer including me also in begining stage  first thinking about why main method is static? why because object is not required to call static method if it were non-static method, jvm create object first then call main() method that will lead the problem of extra memory allocation.So it must be be accessible for an application to run , before any instantiation takes place.

The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.

The static can be:

  1. variable (also known as class variable)
  2. method (also known as class method)
  3. block
  4. static variable

    If we declare any variable as static, it is known static variable.

    • The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc. means a single copy to be shared by all instances of the class.
    • The static variable gets memory only once in class area at the time of class loading.
    • It is a variable which belongs to the class and not to object(instance)
    • Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables
    • A static variable can be accessed directly by the class name and doesn’t need any object

    Advantage of static variable

    It makes our  program memory efficient (i.e it saves memory).

     Ex:static variable

    1. //Program of static variable
    2. class Employee11{
    3.    int empId;
    4.    String name;
    5.    static String company=“VBBS”;
    6.   Employee11(int r,String n){
    7.   empId = r;
    8.    name = n;
    9.    }
    10.  void display (){System.out.println(empId+” “+name+” “+company);}
    11.  public static void main(String args[]){
    12. Employee11 e1 = new Employee11 (111,“Kiran”);
    13. Employee11 e2 = new Employee11 (222,“Aryan”);
    14.  e1.display();
    15.  e2.display();
    16.  }
    17. }

    static method

    static keyword with any method, it is known as 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.
    • static method can access static data member and can change the value of it.

    Example : public static void main itself is a static method

    class MyStatic{
       static int i;
       static String s;
       public static void main(String args[]) //Its a Static Method
       {
           MyStatic obj=new MyStatic();
           //Non Static variables accessed using object obj
           System.out.println("i:"+obj.i);
           System.out.println("s:"+obj.s);
       }
    }

    Output:

    i:0
    s:null
    
    Static method display()
    class MyStatic1{
      static int i;
      static String s;
      //Static method
      static void display()
      {
         //Its a Static method
         MyStatic1 obj1=new Mystatic1();
         System.out.println("i:"+obj1.i);
         System.out.println("i:"+obj1.i);
      }
    
      void funcn()
      {
          //Static method called in non-static method
          display();
      }
      public static void main(String args[]) //Its a Static Method
      {
          //Static method called in another static method
          display();
       }
    }

    Output:

    i:0
    i:0
    
    

    Restrictions for static method

    There are two main restrictions for the static method. They are:
    1. The static method can not use non static data member or call non-static method directly.
    2. this and super cannot be used in static context.
    1. class A{  
    2.  int a=40;//non static  
    3.    
    4.  public static void main(String args[]){  
    5.   System.out.println(a);  
    6.  }  
    7. }        
     

    Static Block

    Static block is mostly used for changing the default values of static variables.This block gets executed when the class is loaded in the memory.
    A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.

    • Is used to initialize the static data member.
    • It is executed before main method at the time of class loading.

    Example: static block

    1. class A2{  
    2.   static{System.out.println(“static block is invoked”);}  
    3.   public static void main(String args[]){  
    4.    System.out.println(“Hello world”);  
    5.   }  
    6. }  

     

    Output:static block is invoked

           Hello world

     

7 thoughts on “What is the static keyword in Java

  • Pingback:Declaring Variables in Java

  • September 16, 2017 at 11:02 am
    Permalink

    Very usefull for me.nice website

    Reply
  • January 10, 2019 at 5:32 pm
    Permalink

    Good way of explaining, and pleasant article to take data on the topic of my presentation focus, which
    i am going to convey in academy.

    Reply
  • April 25, 2019 at 4:39 pm
    Permalink

    I do not even know how I finished up right here, however I thought this
    put up used to be good. I don’t understand who you might
    be however definitely you are going to a famous blogger if you happen to aren’t already.

    Cheers!

    Reply
    • July 8, 2019 at 7:48 am
      Permalink

      Thanks a lot

      Reply
  • July 23, 2019 at 9:39 pm
    Permalink

    Whats up very cool site!! Guy .. Beautiful
    .. Amazing .. I will bookmark your website and take the
    feeds also? I’m happy to find a lot of helpful information right here within the put up,
    we want work out extra strategies in this regard, thank
    you for sharing. . . . . .

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *