Navigation Menu

Currently the navigation bar is not functioning. Use the Blog Archive or the Google Custom Search to find content.

Monday 1 July 2013

Java program based on static members

Program to count the number of objects created for a class using static member.

Code :
class Obj{
    static int count;
    Obj(){
        count++;
        System.out.print("\nObject  "+count+" created...");
    }
    static void showCount(){
        System.out.print("\nTotal Object Count  :  "+count);
    }
}
class ObjCount{
    public static void main(String args[]){
        Obj o1=new Obj();
        Obj o2=new Obj();
        Obj.showCount();
        Obj o3=new Obj();
        Obj.showCount();
    }  
}
Output :
Object  1 created...
Object  2 created...
Total Object Count  :  2
Object  3 created...
Total Object Count  :  3
Note : Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

No comments:

Post a Comment