"Before software can be reusable it first has to be usable"- Ralph Johnson

Friday, April 27, 2012

How Can We restrict someone to create a limited objects of a class

By 12:05 AM
Here is example how we can restrict someone to create a limited objects of a class :-

class Example{ 
    private static int count; 
    private static int MAX_COUNT = 5; 
    private Example(){ 
    } 
      
    public static Example getObject(){ 
        if(count < MAX_COUNT){ 
              count ++; 
              return new Example
(); 
        } 
        else{
            System.out.println("You exceded the limit");
            //TO DO
        } 
    }
Read More...