Wednesday, 9 October 2013

Program to demonstrate static keyword

//Program to demonstrate static keyword
//Developed By Ahlam Ansari
import java.io.*;
import java.util.*;

class Stat
{
    public static int stata= 5;
    public static int statb;
    int a,b;   
    static
    {
        System.out.println("Static Block Initialized");
        statb=10;
    }
    Stat()
    {
    }
    Stat(int x,int y)   
    {
        a=x;
        b=y;
    }
    public static void displayStat()
    {
        stata++;
        statb--;
        System.out.println("\nStatic variables:"+"\nstata="+stata+"\nstatb="+statb);
    }
    void display()
    {
        a++;
        b++;
        System.out.println("\nInstance variables:"+"\na="+a+"\nb="+b);
    }   
}

class MainStat
{
    public static void main(String ahlam[])
    {
        Stat.displayStat();
        Stat.displayStat();
        Stat objS1=new Stat(111,222);
        Stat objS2=new Stat(333,444);
        objS1.display();
        objS2.display();
    }   
}

/*
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac MainStat.java

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MainStat
Static Block Initialized

Static variables:
stata=6
statb=9

Static variables:
stata=7
statb=8

Instance variables:
a=112
b=223

Instance variables:
a=334
b=445

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/

No comments:

Post a Comment