Saturday 2 March 2013

Static Class Members in java


Generally each class contains instance variables and instance methods.  Every time the class is instantiated, a new copy of each of them is created.  They are accessed using the objects with the dot operator.
            If we define a member that is common to all the objects and accessed without using a particular object i.e., the member belong to the class as a whole rather than the objects created from the class.  Such facility can be possible by using “static” keyword.
The members declared with the static keyword are called static members.

Example:         static int x;
                        static int show(int x,int y);
           

            The important points about static class members are
Ø  Static members can be accessible without the help of objects.  It can be accessible by using the syntax:            classname.members;
Ø  A static method can access only static members (data + method)
Ø  A static block will be executed first (before main).  Static variables are initialized first
Ø  A static member data store the same memory for all objects
Ø  Static method cannot be referred to ‘this’ or ‘super’ in anyway.

Example1:

class static1
{
            static int c=0;
            void increment()
            {
            c++;
            }
            void display()
            {
            System.out.println("Value="+c);
            }
}
class Mstatic
{
            public static void main(String[] args)
            {
            static1 s=new static1();
            static1 s1=new static1();
            static1 s2=new static1();
            s.increment();
            s1.increment();
            s2.increment();
            s.display();
            s1.display();
            s2.display();
            }
}

O/P:    value=3
            value=3
            value=3



0
 
0
 
0
 
 

without static keyword              


                                                           s1                           s2                            s3



0
 
 

with static keyword                    

                                                                  s1 s2 s3


Example2:

class static1
{
            static int a=10;
            static int b=34;
            static void call()
            {
            System.out.println("Value1="+a);
            }
}
class Mstatic1
{
            public static void main(String[] args)
            {
            static1.call();
            System.out.println("Value2="+static1.b);
            }
}

O/P:    Value1=10
            Value2=34

Example3:      

class static2
{
            static int a=10,b;
            static void method(int x)
            {
            System.out.println("x="+x);
            System.out.println("a="+a);
            System.out.println("b="+b);
            }
            static
            {
            System.out.println("static block");
            b=a*4;
            }
}
class Mstatic2
{
            public static void main(String[] args)
            {
            static2.method(35);
            }
}

O/P:    static block
            x=35
            a=10
            b=40

No comments:

Post a Comment