REGISTER FREE to Receive Instant Job

Saturday, 1 November 2014

Static Keyword in Java

Static Keyword:
Is used to define class members. In a class we can have following 3 types of class members:-
1- STATIC DATA MEMBERS: Represents class attributes. A single copy of static data members is created when the class is loaded. This copy can be shared by all the objects of the class.
For example -
class A
{
    int a, b;
    static int c;
    -------
    -------   
}
When we create objects of this class in some other class what happens is explained in the given adjoining fig1.

For the execution of a java application memory is divided into three parts called
  • Stack
  • Heap
  • Class Area
LOCAL VARIABLES of methods are stored in STACK. OBJECTS are created in the HEAP and STATIC DATA MEMBERS are saved in CLASS AREA.
 
2- STATIC INITIALIZE BLOCK
3- STATIC METHODS.

class A
{
    int a, b;
    static int c;
    ----------   
    ----------
    public static void main(String[] args)
    {
        A x = new A();
        A y = new A();
        -------------
-------------
    }
}

Static Initializer Block- is used to initialize static data members of a class.
Syntax –
static
{
Statements
}
Static initialize is executed only once, just after a class is loaded.
In Java, a class can be loaded in either of the following ways:-
  1. Through Explicit Introduction.
  2. Through Implicit References.
In the first approach class to be loaded is explicitly introduced to the JRE.

Only a single class is loaded in an application using explicit introduction. Rest of the classes are loaded through implicit reference.
When a reference of a class is encountered first time in an already loaded class then JRE implicitly loads it before performing the operation represented by the class reference.
class A
{
    public static void main(String[] args)
    {
        B x = new B();//Reference of B is contained in A
        -------------
-------------
    }
}

class B
{
 -------------
 -------------
}

At the time of class loading following sequence of steps is performed by JRE:-
1.    Static Data Members (if defined) are created in the Class Area.
2.    Static Initializer Block (if defined) is executed.
3.    Reference of the class which resulted in class loading is resolved i.e. operation represented by the reference is performed.

class A
{
    static
    {
        System.out.println(“A is loaded.”);
    }
   
    public A()
    {
        System.out.println(“A is instantiated.”);
}
}

class B
{
    static int b;
   
static
    {
        b=5;
System.out.println(“B is loaded.”);
    }
}

class C
{
    static
    {
System.out.println(“C is loaded.”);
    }
   
    public static void display()
{
System.out.println(“Display() of C is invoked.”);
}
}
   
class D
{
    static
    {
System.out.println(“D is loaded.”);
    }
   
    public static void main(String args[])
{
System.out.println(“Instantiating A………”);
A x = new A();
System.out.println(“Referencing static data member b of B……”);
System.out.println(“b of B is : “ + B.b);
System.out.println(“Invoking static method of C……”);
C.display();
System.out.println(“Instantiating A again.”);
A y = new A();
}
}

-------------------------------------------------------------------------------------------------------
class E
{
    static int a = 5;
   
    public static void main(String args[])
{
System.out.println(“a = “ + a);
}
}



class E
{
    static int a;
   
    static
{
a = 5;
}
----
----
}
-------------------------------------------------------------------------------------------------------


class E
{
    int a = 5, b=6;
   
    public static void main(String args[])
{
E x = new E();
System.out.println(“a = “ + x.a);
System.out.println(“b = “ + x.b);

}
}



class E
{
    int a, b;
   
E()
    {
a = 5;
b = 6;
}
----
----
}
-------------------------------------------------------------------------------------------------------
class E
{
    int a=5, b=6;
   
    public E()
    {
        System.out.println(“Default.”);
}

    public E(int x)
    {
        a=x;
        System.out.println(“One parameterized.”);
}

    public E(int x, int y)
    {
        b=y;
        System.out.println(“Two parameterized.”);
}

public void display()
{
System.out.println(“a = “ + a);
System.out.println(“b = “ + b);
}

    public static void main(String args[])
{
E x = new E();
E y = new E(10);
E z = new E(40, 50);
x.display();
y.display();
z.display();
}
}


class E
{
    int a, b;
   
    public E()
    {
        a=5, b=6;
        System.out.println(“Default.”);
}

    public E(int x)
    {
        a=5, b=6;
        a=x;
        System.out.println(“One parameterized.”);
}

    public E(int x, int y)
    {
        a=5, b=6;
        b=y;
        System.out.println(“Two parameterized.”);
}

public void display()
{
System.out.println(“a = “ + a);
System.out.println(“b = “ + b);
}

    public static void main(String args[])
{
E x = new E();
E y = new E(10);
E z = new E(40, 50);
x.display();
y.display();
z.display();
}
}

Limitations of static methods & static block:-
  • Only static data members of a class can be referred in a static block or method.
  • A static block or static method can directly invoke only static methods.
  • ‘this’ and ‘super’ keyword cannot be used in a static method or in  a static block.
Program below executes without ‘main’ method –
class Test
{
    static
    {
System.out.println(“It is executing without main…..”);
System.exit(0);
}
}

Not only we can do this much but also we can do list of things. See the program below : -

class Test2
{
    int a;
   
    public Test2(int x)
    {
        a = x;
    }

    public void display1()
    {
        System.out.println("a = " + a);
    }

    static
    {
        System.out.println("Test is loaded.");
        Test2 t = new Test2(5);
        t.display1();
        P x = new P();
        System.exit(0);
    }
}

class P
{
    public P()
    {
        System.out.println("P is instantiated.");
    }

    static
    {
        System.out.println("P is loaded.");
    }
}

2 comments:

Popular Posts

Powered by Blogger.

About Me

My photo

JobsAcid is a 'job search engine' to provide finding a job on the internet easier for the user. It has designed as huge selection of job offerings on the same search engine.