How can we make a class Singleton ?
 A) If the class is Serializable
class Singleton implements Serializable
{
private static Singleton instance;
private Singleton() { }
public static synchronized Singleton getInstance()
{
if (instance == null)
instance = new Singleton();
return instance;
}
/**
If the singleton implements Serializable, then this
* method must be supplied.
*/
protected Object readResolve() {
return instance;
}
/**
This method avoids the object fro being cloned
*/
public Object clone() {
throws CloneNotSupportedException ;
//return instance;
}
}
B) If the class is NOT Serializable
class Singleton
{
private static Singleton instance;
private Singleton() { }
public static synchronized Singleton getInstance()
{
if (instance == null)
instance = new Singleton();
return instance;
}
/**
This method avoids the object from being cloned
**/
public Object clone() {
throws CloneNotSupportedException ;
//return instance;
}
}    
How is static Synchronization different form non-static synchronization?
  When Synchronization is applied on a static Member or a static block,  the lock is performed on the Class and not on the Object, while in the  case of a Non-static block/member, lock is applied on the Object and not  on class. [Trail 2: There is a class called Class in Java whose object  is associated with the object(s) of your class. All the static members  declared in your class will have reference in this class(Class). As long  as your class exists in memory this object of Class is also present.  Thats how even if you create multiple objects of your class only one  Class object is present and all your objects are linked to this Class  object. Even though one of your object is GCed after some time, this  object of Class is not GCed untill all the objects associated with it  are GCed.
This means that when ever you call a "static synchronized" block, JVM  locks access to this Class object and not any of your objects. Your  client can till access the non-static members of your objects. 
What are class members and Instance members?  
Any global members(Variables, methods etc.) which are static are called  as Class level members and those which are non-static are called as  Instance level members. 
Name few Garbage collection algorithms? 
  Here they go:
Mark and Sweep
Reference counting
Tracing collectors
Copying collectors
Heap compaction
Mark-compact collectors
Can we force Garbage collection?  
java follows a philosophy of automatic garbage collection, you can  suggest or encourage the JVM to perform garbage collection but you can  not force it. Once a variable is no longer referenced by anything it is  available for garbage collection. You can suggest garbage collection  with System.gc(), but this does not guarantee when it will happen. Local  variables in methods go out of scope when the method exits. At this  point the methods are eligible for garbage collection. Each time the  method comes into scope the local variables are re-created.
No comments:
Post a Comment