javascript1

Friday, 13 February 2015

Static Variable , Static Method and Static Block

Static Keyword 


The static keyword is is used for  memory management . java static  keyword can be used  with  a variables , methods , blocks and nested class. When we define a static keyword with method and varriable then it is related to class.
 The static can be:
  1. A class member (varriable)
  2. A method of class
  3. A block
  4. A nested class (class within class)

1) Java static variable
Static varriable are  those varriable which are related to class instead of  objects .
Static varriable  gets the memory once  at time of class loading  not at the time of object creation .
Advantage of static variable
 static is used for efficient utilization of memory.

Understanding problem without static variable


class Employee
{  
     int Eid;  
     String name;  
     String company="google";  

Let us assume there are 10,000 employee`s  in company , now all data members of class  get memory each time when object of class  is created. All employye`s have its unique Eid and name so i  should not be static because it is related to every employee but company name is  common for all employees . If we make company name static than it takes  memory only once


Example of static variable
//Program of static variable  
  
class employee{  
   int eid;  
   String name;  
   static String company ="google";  
     
   employee(int a,String b){  
   eid = a;  
   name = b;  
   }  
 void display ()
{
System.out.println(eid+" "+name+" "+company);}  
  
 public static void main(String args[])
{  
 employee e1 = new employee(1200,"vinay");  
 employee e2 = new employee(1201,"abhishek");  
   
 s1.display();  
 s2.display();  
 }  


class Javacount {  
int i=0;// i  gets the  memory when object of a class is created   
  
Javacount(){  
i++;  
System.out.println(i);  
}  
  
public static void main(String a[]){  
  
Javacount c1=new Javacount ();  
Javacount c2=new Javacount ();  
Javacount c3=new Javacount ();  
  
 }  



Output:1
       1
       1


class Javacount {  
static int i=0;// i  gets the  memory when object of a class is created   
  
Javacount(){  
i++;  
System.out.println(i);  
}  
  
public static void main(String a[]){  
  
Javacount c1=new Javacount ();  
Javacount c2=new Javacount ();  
Javacount c3=new Javacount ();  
  
 }  

Output:1
       2
       3
Where static method can’t be used

1.    static method is not able to acces non static data members and non static method.
2.    In  static method this and super keyword is not used .

1.    class employee{  
2.     int value=40;           // non static data member   
3.       
4.     public static void main(String a[]){  
5.      System.out.println(value);  
6.     }  
7.    }  

Output:Compile Time Error
Java static block
  • static block  is  used for  static data member initialization .
  • static block executed before the main method .
Example of static block
1.    class employee
2.    {  
3.      Static
4.    {
5.    System.out.println("this is static block of employee class");}  
6.      
7.    public static void main(String argument[])
8.    {  
9.       
10. System.out.println("main funtion is invoked ");  
11.  
12.   }  
13. }
 


No comments:

Post a Comment