Constructor in abstract class



Can we create constructor in abstract class ?
yes we can .



What is the difference following statement between?

List myList = new ArrayList(); and 
ArrayList myList = new ArrayList();

Above declarations look exactly the same in a short program, but if you go on to use 
myList 100 times in your program you can start to see a difference. The first 
declaration ensures that you only call methods on myList that are defined by the 
List interface (so no ArrayList specific methods).
 
If you've programmed to the interface this way,
later on you can decide that you really need

List myList = new TreeList();
and you only have to change your code in that one spot. 
You already know that the rest of your code doesn't do anything 
that will be broken by changing the implementation because 
you programmed to the interface. 

Break singleton pattern


What are the different ways we can break a singleton pattern in Java?

  • Use reflection to access the private constructor and instantiate the class as many times as you want.(can prevent using early initialization)
  • Serialization. If you do not implement readResolve then reading a singleton with
          ObjectInputStream.readObject() will return a new instance of this singleton.
  • Clone() :Preferred way is not to implement Cloneable interface. 

double locked Singleton

How to write double locked Singleton class in Java ?

we can check double locked in java singleton class as below code

public class SingletonInstanceDemo {
    private static volatile SingletonInstanceDemo instance = null;
    // private constructor
    private SingletonInstanceDemo() {
    }
    public static SingletonInstanceDemo getInstance() {
        if (instance == null) {
            synchronized (SingletonInstanceDemo.class) {
                // Double check
                if (instance == null) {
                    instance = new SingletonInstanceDemo ();
                }
            }
        }
        return instance;
    }
}

What is Perm space


It stands for permanent generation

Perm space is used to keep information for loaded classes and few other advanced features like String Pool(for highly optimized string equality testing), which usually get created by String.intern() methods. As your application(number of classes) will grow this space shall get filled quickly, since the garbage collection on this Space is not much effective to clean up as required, you quickly get Out of Memory : perm gen space error. After then, no application shall run on that machine effectively even after having a huge empty JVM.
Before starting your application you should java -XX:MaxPermSize to get rid of this error.
Permanent generation is special because it holds meta-data describing user lasses (classes that are not part of the Java language).
Examples of such meta-data are objects describing classes and methods and they are stored in the Permanent Generation.