Scala tutorials: How write a Singleton Class in scala and java

Scala tutorials: How write a  Singleton Class in scala and java

As we know singleton is class is widely and commonly used design pattern.This class provides single instance of the class at any given point of time,throughout the JVM instance

Usually in java we create singleton class by writing a private constructor, and declaring static variable named instance and initiate the class to that variable and followed by writing a static getter method for instance variable to return the class object.

Example code of java singleton class

public class ExampleSingleton {
    private static ExampleSingleton ourInstance = new ExampleSingleton();

    public static ExampleSingleton getInstance() {
        return ourInstance;
    }

    private ExampleSingleton() {
    }
}

The same Singleton Class in scala is very Simple and easy, we write object and define the functions which are required that's it.......

Scala Singleton class example

object SingletonClassExample {
    def damIameasy(){
      println("remember mutable and immutables..!!!!!");
    }
}

usage

SingletonClassExample.damIameasy();

time for cofee break...!!!coffee_gif_by_javaleaf-dbfc7gt