Saturday 28 July 2012

Static Variables and Methods


class Rectangle
{
        int length;
        int breadth;
        private static int count;
        static void displaycount()
        {
                System.out.println("No of Object : " + count);
        }
        Rectangle(int a,int b)
        {
                length = a;
                breadth = b;
                count++;
        }
        Rectangle()
        {
                length = 0;
                breadth = 0;
                count++;
        }
        void display()
        {
                System.out.println("Length : " + length);
                System.out.println("Breadth : " + breadth);
        }
        public static void main(String args[])
        {
                Rectangle.displaycount();
                Rectangle r1 = new Rectangle(10,20);
                Rectangle.displaycount();
                Rectangle r2 = new Rectangle();
                Rectangle.displaycount();
            r1.display();
            r2.display();    
        }
}

1 comment: