samedi 14 janvier 2017

getting exception on below singleton in java

I have given this below problem

Complete the Singleton class in your editor which contains the following components:

A private Singleton non parameterized constructor.
A public String instance variable named .
Write a static method named getSingleInstance that returns the single instance of the Singleton class.

Once submitted, our hidden Solution class will check your code by taking a String as input and then using your Singleton class to print a line.

Input Format

You will not be handling any input in this challenge.

Output Format

You will not be producing any output in this challenge.

Sample Input

hello world

Sample Output

Hello I am a singleton! Let me say hello world to you

for this challenge I have designed the below class

class Singleton  implements Cloneable,Serializable {

public static volatile Singleton str = null; 

   private  Singleton ()
   {

       if (str!=null)
       {
           throw new IllegalStateException("object already instaniated");
       }
   }



   public static Singleton getSingleInstance()
   {
       if (str==null)
           synchronized (Singleton.class)
           {
               if (str==null)
               {
                   str= new Singleton(); 
               }

        }
       return str ;
   }}

but now i am getting this below exception please advise how to overcome from this

Main.java:66: error: incompatible types
    s1.str=str;
           ^
  required: Singleton
  found:    String
Main.java:67: error: incompatible types
    s2.str=str;
           ^
  required: Singleton
  found:    String
2 errors

folks please advise

Aucun commentaire:

Enregistrer un commentaire