Architecture, Java technology news, interviews , updates , trends, frameworks..
Wednesday, May 20, 2009
Ways to implement singleton pattern in Java
A simple and less confusing way to implement this is
public class
{
public final static Bar bar = new Bar();
private Bar()
{
// This prevents class instantiation from outside
}
public static Bar getBar()
{
return bar;
}
}
This is cleaner and readable code, but it does not give us an option of lazy initialization.
When you have a very large object or heavy construction code AND also have other accessible static methods or fields that might be used before an instance is needed, then and we need the lazy initialization option.
public class Bar
{
public final static Bar bar ;
private Bar()
{
// This prevents class instantiation from outside
}
public static Bar getBar()
{
synchronized(Bar.class)
{
if(bar == null)
{
bar = new Bar();
}
return bar;
}
}
}
The synchronized block helps to keep this instance creation thread safe.
If we are using Java 5 or above, we can use the additional feature provided by it for Double Checked locking. Without this, other threads are not guaranteed by the JMM ( Java Memory Model) to see the changes to its value.
You can refer this classic article on this subject:
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
public class Bar
{
public static volatile Bar bar ;
private Bar()
{
// This prevents class instantiation from outside
}
public static Bar getBar()
{
synchronized(Bar.class)
{
if(bar == null)
{
bar = new Bar();
}
return bar;
}
}
}
Note the volatile keyword which does the trick.
Adding
protected Object clone()
{
throw new CloneNotSupportedException();
}
Would make Singletons better !!.
we have to choose the design based on requirement and make use of Singletons very carefully in our applications.
Cheers
Vinay
Thursday, April 16, 2009
What to know as a Java Developer?
For a java developer, we should build our base knowledge around fundamentals of Java language and be strong in java interfaces, generics, annotations, concurrency etc., It's easy to learn quickly new technologies when you master the fundamentals.
Here is a list of things which (out of many things) a java developer must be aware of.
1. How does JVM work?
2. Improvements on language from major version changes (Generics, Annotations, Enums ...).
3. What are the OOPS concepts and how do they Implemented in Java?
4. Understand design pattern
5. Coding Conventions.
6. Build tool (Ant) or Project Management Tool (Maven).
7. At least basic knowledge of some popular frameworks such as Struts, springs, hibernate.
8. Writing test cases using Junit.
9. Good knowledge of Eclipse ( or any popular IDE ).
10. Knowledge of using log4j.
Saturday, April 4, 2009
why not ALWAYS use ArrayLists in Java?
If you need a collection of primitives, then an array may well be the best tool for the job. Boxing is a comparatively expensive operation. For a collection (not including maps) of primitives that will be used as primitives, I almost always use an array to avoid repeated boxing and unboxing.
I rarely worry about the performance difference between an array and an ArrayList
, however. If a List
will provide better, cleaner, more maintainable code, then I will always use a List
(or Collection
or Set
, etc, as appropriate, but your question was about ArrayList
) unless there is some compelling reason not to. Performance is rarely that compelling reason.
Using Collection
s almost always results in better code, in part because arrays don't play nice with generics, as Johannes Weiß already pointed out in a comment, but also because of so many other reasons:
- Collections have a very rich API and a large variety of implementations that can (in most cases) be trivially swapped in and out for each other
- A Collection can be trivially converted to an array, if occasional use of an array version is useful
- Many Collections grow more gracefully than an array grows, which can be a performance concern
- Collections work very well with generics, arrays fairly badly
- As TofuBeer pointed out, array covariance is strange and can act in unexected ways that no object will act in. Collections handle covariance in expected ways.
- arrays need to be manually sized to their task, and if an array is not full you need to keep track of that yourself. If an array needs to be resized, you have to do that yourself.
All of this together, I rarely use arrays and only a little more often use an ArrayList
. However, I do use List
s very often (or just Collection
or Set
). My most frequent use of arrays is when the item being stored is a primitive and will be inserted and accessed and used as a primitive. If boxing and unboxing every become so fast that it becomes a trivial consideration, I may revisit this decision, but it is more convenient to work with something, to store it, in the form in which it is always referenced. (That is, 'int' instead of 'Integer'.)