Static Keyword in Java

The static keyword in Java is used for memory management mainly.In Java Static keyword is used with :
Some Points Regarding Static Keyword in Java
  • Static fields are initialized at time of class loading in Java,Opposite to instance variable which is initialized when you create instance of a particular class.
  1. ) Variable
  2. ) Method
  3. ) Blocks
  4. ) Nested Class
1.) Java Static Variable
If we used a static keyword with a variable then this variable is known as static variable

Some Points Regarding Static Variable :
  • Static Variable is used to refer common property of all objects.
  • Static variable gets memory only once in class area at time of class loading.
  • Static variable makes our program more efficient.
Example :

public class StaticVariable {

    int roll_no;
    String name;
    static String college="PIET";

    //Create Parameterize Constructor 

    StaticVariable(int r,String n){
        roll_no=r;
        name=n;
        
    }
    void display(){
        System.out.println("name :"+name+",roll_no :"+roll_no+",college :"+college+"");
    }
    public static void main(String[] args) {
        StaticVariable sb1=new StaticVariable(23,"Mohan");

        StaticVariable sb2=new StaticVariable(12,"Raju");

        sb1.display();
        sb2.display();
    }
}

OutPut :
2.) Java static Method
If we apply a static keyword with any method then it is known as static method.
Some Points Regarding Static Method :

  • A static method belongs to the class rather than object of a class.
  • Static Method can access only static data member.
  • Static Method can not access Non-static data member.
  • this and super keyword are not used in static context.
Note : When we want to access  Non static member inside a static method then we obtain Compile time error,.

Example :




public class StaticMethod {

    String name="Synapse";

    public static void main(String[] args) {

     System.out.println("Hello "+name);
   
 }
    
}

OutPut :


During this we obtain compile time error and to remove this problem we use static keyword which is initialize outside static method.
Example :



public class StaticMethod {

    static String name="Synapse";

    public static void main(String[] args) {

     System.out.println("Hello "+name);
    
}   
    

}

OutPut :

Question : Why Java main method is static?
Answer :Because Object is not require to call a static method.

3.) Java Static block

Static block is executed before main method i.e at time of Class loading.In case of Static block we create a static block by using keyword static as shown in Example below :
Example :


public class StaticBlock {

    static{
        System.out.println("Static Block :");
    }

    public static void main(String[] args) {
        System.out.println("Main Method");
    }
    
}

OutPut :

Question : Can we execute a class without main method ?

Answer : Yes we can ,By help of static block but for this purpose our JDK version should not be 1.7 but it can be only possible in previous version of JDK.

public class HelloWorld{
     
    static{
        System.out.println("Hello World");
    }
}

Note: You can not be able to execute this code in JDK 1.7 or onward it can be only executable only in previous version of JDK.
Question : Can we override static methods in Java ?

Answer : No, we can't override static method in Java because static method is bounded with class.
Explanation :
 When parent class and child class contains a method with same name, same number of arguments and same signature type then in that case method is known as overridden Method.Method overridden is associated with object where as static method is bound to a class.

Comments

Popular posts from this blog

Secure Database Connectivity in node.js with mysql

Export data from mysql db to csv file using java

API (Application Programming Interface)