Thursday, December 30, 2010

Use of static keyword in bluej programs

Among several keywords in java, static is a keyword mainly used in functions and variables. Any variable declared in as a data member are normally instance variable. The meaning of instance variable means those variables whose existence depends on the creation of an object. After any object is created, instance variables come into existence. Another feature of instance variable is that for each and every object, separate copies of instance variable are created.

So far as functions are concerned, they are also known as instance function so far as static keyword is not used. Normally for any number of objects, only single copy of functions of that particular class is created. Existence of instance functions also depends upon the creation of the object. I think students have observed the feature that whenever the function readLine() of BufferedReader class is called, it is called after creating the object of BufferedReader class as readline() is an instance function.

Use of static in variable



Whenever we declare any variable as static, it becomes the class member and its existence does not depend upon the creation of object of that class. In a word static variable can be used before creating the object and it is accessed using the class name. Number of objects may be created of that class but unlike instance variable, only one instance of static variable will be created. All the objects of that class will deal with the same copy.

class Stat
{
int a;
static int b;
void set(int x,int y)
{
a=x;
b=y;
}
void display()
{
System.out.println("Values are="+a+","+b);
}
public static void main(String args[])
{
Stat ob1,ob2;
ob1=new Stat();
ob2=new Stat();
ob1.set(2,4);
ob2.set(10,20);
System.out.println("First object");
ob1.display();
System.out.println("Second Object");
ob2.display();
}
}

Here in the above program ‘int a’ is instance variable while ‘int b’ is the static one. Although in this program the static variable is accessed after creating objects but it can be accessed before creating objects. The point to be noted here is that for both the objects, the static member is common. While assigning value through set() function, for the first object the value is 4 and for the second object it is 20. As the static member is common for both the objects, last assigned value 20 will be the ultimate value of the static variable ‘int b’. Displaying the values through display() function, we will find that for both the objects, the static variable ‘int b’ is 20 while ‘int a’ will contain different values for the objects.

What is static method or function ?



Static method or static function means whose existence comes before creating any object of that class and can be accessed using the class name.

class Stat
{
int a;
static int b;
static void setStatic(int y)
{
b=y;
/* instance variable can not be accessed here */
}
void set(int x)
{
a=x;
/* static variable can be accessed here */
}
void display()
{
System.out.println("Values are="+a+","+b);
}
public static void main(String args[])
{
Stat ob1,ob2;
ob1=new Stat();
ob2=new Stat();
Stat.setStatic(100);
ob1.set(2);
ob2.set(10);
System.out.println("First object");
ob1.display();
System.out.println("Second Object");
ob2.display();
}
}

In this above program, just see how the function ‘setStatic()’ is called. It can be called using the object reference also. Keep the following points in mind for static method
1. Non static members can not be accessed within static function body while non static or instance functions can access static members.
2. ‘this’ keyword can not be used within static function body.

4 comments:

  1. thank you very much. this did clear out my doubts, you have very clear concepts, i must say!

    ReplyDelete
  2. import java.io.*;
    public class stack
    {
    int top=-1;
    public void push(int a[],int n) throws IOException
    {
    int val;
    System.out.println("Enter value to push : ");
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    val=Integer.parseInt(br.readLine());
    if(top < n-1)
    a[++top]=val;
    else
    System.out.println("\nStack overflow.... \n");

    }
    public void pop(int a[])
    {
    int val;
    if(top==-1)
    System.out.print("\nStack Underflow...\n");
    else
    {
    val=a[top];
    System.out.print("\nDeleted element : " + val);
    top--;
    }

    }
    public void display(int a[])
    {
    int i;
    if(top==-1)
    System.out.print("\nStack Underflow ...\n");
    else
    {
    System.out.print("\nStack : ");
    for(i=top;i>=0;i--)
    System.out.print(a[i] + " ");
    }
    }
    public void main(String args[]) throws IOException
    {
    System.out.println("Program to implement stack : \n");
    int n,ch;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter array capacity : ");
    n=Integer.parseInt(br.readLine());
    int a[]=new int[n];
    do
    {
    System.out.println("[1] Push [2] Pop [3] Display [4] Exit\n");
    System.out.print("\nEnter your choice : ");
    BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
    ch=Integer.parseInt(br1.readLine());
    switch(ch)
    {
    case 1:
    push(a,n);
    display(a);
    break;

    case 2:
    pop(a);
    display(a);
    break;

    case 3:
    display(a);
    break;

    case 4:
    break;

    default :
    System.out.println("Enter correct choice...");
    break;
    }

    }while(ch!=4);
    }
    }


    Kindly help me with this program....

    ReplyDelete
  3. Check it:


    import java.io.*;
    public class stack
    {
    int top=-1;
    public void push(int a[],int n) throws IOException
    {
    int val;
    System.out.println("Enter value to push : ");
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    val=Integer.parseInt(br.readLine());
    if(top < n-1)
    a[++top]=val;
    else
    System.out.println("\nStack overflow.... \n");

    }
    public void pop(int a[])
    {
    int val;
    if(top==-1)
    System.out.print("\nStack Underflow...\n");
    else
    {
    val=a[top];
    System.out.print("\nDeleted element : " + val);
    top--;
    }

    }
    public void display(int a[])
    {
    int i;
    if(top==-1)
    System.out.print("\nStack Underflow ...\n");
    else
    {
    System.out.print("\nStack : ");
    for(i=top;i>=0;i--)
    System.out.print(a[i] + " ");
    }
    }
    public static void main(String args[]) throws IOException
    {
    System.out.println("Program to implement stack : \n");
    int n,ch;
    stack ob=new stack();
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter array capacity : ");
    n=Integer.parseInt(br.readLine());
    int a[]=new int[n];
    do
    {
    System.out.println("[1] Push [2] Pop [3] Display [4] Exit\n");
    System.out.print("\nEnter your choice : ");
    BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
    ch=Integer.parseInt(br1.readLine());
    switch(ch)
    {
    case 1:
    ob.push(a,n);
    ob.display(a);
    break;

    case 2:
    ob.pop(a);
    ob.display(a);
    break;

    case 3:
    ob.display(a);
    break;

    case 4:
    break;

    default :
    System.out.println("Enter correct choice...");
    break;
    }

    }while(ch!=4);
    }
    }

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner