Thursday 30 January 2014

Avoid cloning Singleton object -Java

HI Viewers
 Here i'm going to give a short note one how to avoid a singleton object from getting cloned.This is one of the interview questions that are being asked frequently while interviewing .We all know that singleton pattern is which doesn't allow for invoking new keyword while creating new instance to that class.generally we achieve it in many ways that i will explain in Singleton design pattern .Here I'm giving you a small code snippet which doesn't allow Singletons instance from getting cloned.

Create a class Singleton and override the Object class clone method and return CloneNotSupported instance to that method
public class Singleton implements Cloneable{
 private static Singleton ss=new Singleton();
 
 private  Singleton() {

 }

 public static Singleton getInstance(){
  return ss;
 }
  public Object clone(){
   
  return new CloneNotSupportedException();
   
  }

}
Now create a sample class to check the Singleton class clone mechanism
public class Example {
public static void main(String args[]){
 
 Singleton ss=(Singleton) Singleton.getInstance().clone();
}
}
Out put: Exception in thread "main" java.lang.ClassCastException: java.lang.CloneNotSupportedException cannot be cast to Singleton

Tuesday 21 January 2014

UBUNTU-System Stuck in Login Loop

Hi Ubuntu users,you might some times face these problem like when you try to log in with proper credentials it will get logged in immediately a black screen appears. Weird thing without even you see the content on the black screen your machine again show you log in screen.And the process repeats and make you vexed.Here is the solution which i found and made may machine work .


Step 1: Press ctrl+alt+F3 in your machine .you will get shell screen log in to it .

Step2 : Run ls-lah command in the shell .Check for .Xauthority in the output shown.If the output is like
           -rw------- 1 root root 53 Jan 21 10:19 .Xauthority


Step 3: Do  sudo chown username:username .Xauthority and try logging in .It worked for me and               try  this yourselves if you got stuck in the similar way


Thanks and Regards
JavasimpleStuff