Tuesday, 25 February 2014

Static in Java



Static is a keyword which can be applied to a variable, method and block. So in Java there are static variable, static method and static block.

1- Static variable-

Static variable is belong to class rather than (object) instance of the class. It means that only one instance of a static field exists even if you create a million instances of the class or you don't create any. It will be shared by all instances.

Static variables are mainly used for memory management. Static variable get the memory only once, if any object changes the value of the static variable, it will retain its value.

When we'll use static keyword before a variable that variable will be known as static variable.

Ex1-

class Test{ 
static int i=0;
 
Test(){ 
i++; 
System.out.println(i); 
 
public static void main(String args[]){ 
 
Test t1=new Test(); 
Test t2=new Test(); 
Test t3=new Test(); 
 
}} 

Output-
1
2
3


2- Static Method-

When we'll use static keyword before a method that method will be known as static method. Static method is belong to class rather than (object) instance of the class. We'll call a static method by class name.

Restrictions-
1-Static method can access only static data.
2-this and super keyword can not be use in static method.


Ex2-

class Test{ 
  static int add(int a, int b, int c){ 
  return a+b+c; 
  } 
 
  public static void main(String args[]){ 
  int sum=Test.add(1,2,3); 
  System.out.println(sum); 
  } 

Output-
6

Ex3-

class Employee{ 
     int empId; 
     String empName; 
     static String orgnization = "IBM"; 
      
     static void test(){ 
     orgnization = "Oracle"; 
     } 
 
     Employee(int e, String n){ 
     empId = e; 
     empName = n; 
     } 
 
     void showData ()
     {
     System.out.println(empId +" - "+empName+" - "+orgnization);
     } 
 
    public static void main(String args[]){ 
    Employee.test(); 
 
    Employee e1 = new Employee (4231,"Mark"); 
    Employee e2 = new Employee (4232,"Stephen"); 
    Employee e3 = new Employee (4233,"David");
    Employee e4 = new Employee (4244,"Kevin"); 
 
    e1.showData();
    e2.showData();
    e3.showData();
    e4.showData();
    } 


Output:
4231 Mark Oracle
4232 Stephen Oracle
4233 David Oracle
4234 Kevin Oracle


3- Static block-


When we use static before a block of code or simply a block such block is known as static block.
Static block always execute before main method.

Ex4-
public class StaticBlockDemo
{
// static block begin
  static
  {
  System.out.println("Hello");
  }

// static block end

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

}

Output-
Hello World



IQ) why main method is static?
Ans- No, Object is required to call static method. That’s why main method is static.  if it was a non-static method, then jvm create object first then call main() method that will lead the problem of extra memory allocation.

IQ) Can we overload main method?

Ans- Yes, Main method in Java can be overloaded like any other method in Java but JVM will only call main method with specified signature as public static void main(String args[]).

No comments:

Post a Comment