Static Keyword
  • Static means class level. static can be applied to variable method and block.
  • static variables , static methods , static blocks.
  • makes your program memory efficient 
Static variables 
  • If you 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.
  • The static variable gets memory only once in class area at the time of class loading.
Example program:

package com.instanceofjava.snippets.basics;


public class StaticDemo {

    // static variable - the same on all instances of a class
    static int X = 10;

    // non static variable
    int Y = 5;
   
    public static void main(String[] args) {




  StaticDemo o1= new StaticDemo();

  StaticDemo o2= new StaticDemo();


  System.out.println("o1.X = " + o1.X + " o1.Y = " + o1.Y);

  System.out.println("o2.X = " + o2.X + " o2.Y = " + o2.Y);

 

  instance1.X = 15;

  instance1.Y = 10;

 

  System.out.println("After updating X value to 15 and Y value to 10 from o1:");

  System.out.println("o1.X = " + o1.X + " o1.Y = " + o1.Y);

  System.out.println("o2.X = " + o2.X + " o2.Y = " + o2.Y);

    }

}

Output:

instance1.X = 10 o1.Y = 5
instance2.X = 10 o2.Y = 5
After updating X value to 15 and Y value to 10 from o1:
instance1.X = 15 o1.Y = 10
instance2.X = 15 o2.Y = 5 



Static methods:
  • Static method belongs to the class rather than object of a class
  • Static methods can be called without creating an object of class
  • Static method can access static data member and can change the value of static data
  • You can define as many static methods. These methods are independent, except that they may refer to each other through calls
  • Any static method can call any other static method in the same file or any static method in a Java library like Math
Example program:

  1. class Student{  
  2.      int rollno;  
  3.      String name;  
  4.      static String college = "SBIT";  
  5.        
  6.      static void change(){  
  7.      college = "SBCE";  
  8.      }  
  9.   
  10.      Student9(int r, String n){  
  11.      rollno = r;  
  12.      name = n;  
  13.      }  
  14.   
  15.      void show(){
  16.      System.out.println(rollno+" "+name+" "+college);
  17.    }  
  18.   
  19.     public static void main(String args[]){  
  20.     Student.change();  
  21.   
  22.     Student s1 = new Student (37,"indhu");  
  23.     Student s2 = new Student (38,"sindhu");  
  24.     Student s3 = new Student (39,"swathi");  
  25.   
  26.     s1.show();  
  27.     s2.show();  
  28.     s3.show();  
  29.     }  

OutPut:

indhu SBCE
sindu SBCE
swathi SBCE

Static Blocks:
  • Whenever class loads these static blocks will be exeuted.
  • Static blocks are also called Static initialization blocks . A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword.
  • we can use static blocks to assign static variables.
Example program:
class staticBlockDemo{

    static int i;

    int j;

    

    // start of static block

    static {

        i = 20;

        System.out.println("static block called ");

    }

    // end of static block

}

class Main {

    public static void main(String args[]) {

        // Although we don't have an object of staticBlockDemo, static block is

        // called because i is being accessed in following statement.

        System.out.println(staticBlockDemo.i);

    }

}



      Output:

static block called

20



 

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

1 comments for Static Keyword

Select Menu