Skip to main content


Showing posts from January, 2014

Enhancement for JAVA Enhanced for loop

If you are coding in Java many time you had met well Known Exception while using enhanced for each loop.

Null Pointer Exception is unavoidable if you use Enhanced For Each Loop without checking NULL condition for your collection.


List<Object> objectList=null; for (Object object :objectList) { //operation on Object }
Every time we have to check for Collection not to be Null.

To remove this ugly practice design utility method to check for NULL.

publicstatic<T> List<T> safe( List<T> list ){return list ==null? Collections.EMPTY_LIST: list;}
Note that Collections.EMPTY_LIST will avoid allocating an extra object.It is immutable Object that can not be modified.

By using utility method you can avoid Null Pointer Exception .

List<Object> objectList=null;for(Object object : safe(objectList)){//operation on Object }

Thank you.

Design Pattern : Reusable OO Design

What is meant by design Pattern?

From Wiki Definition , in software engineering, a design pattern is a general reusable solution to a commonly occurring problem
within a given context in software design.

What is meant by that?

Suppose there is a team of four to five people working on ABC project for their client X.
After successful delivery Client wants some changes to project.
Some changes not a Big Deal.While incorporating that changes team stuck to many problems such as changing some Model Diagram Structure,Some interface,Moving methods of Class,Object creation.
After thinking so much, they have designed Specific template to solve this problem and incorporate it in project.

Then there is another team working on XYZ project for client A.But this team member knows that ABC project team have specific template.With the help of that design this team understood that in futer we may stuck with same problem that earlie team had stuck.
Hence they incorporated the changes.After successful de…