Akshay
Akshay

Reputation: 2763

Explanation of java instance

Can anyone tell me what does this thing do? Also if anyone can give an example if would be helpful.

public class ConnectionManager{
    private static ConnectionManager instance = null;
  .....}

Here is the complete code:

package com.gollahalli.main;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager
{
    private static ConnectionManager instance = null;

    private final String USERNAME = "root";
    private final String PASSWORD = "root";
    private final String H_CONN_STRING = "jdbc:hsqldb:data/explorecalifornia";
    private final String M_CONN_STRING = "jdbc:mysql://localhost/explorecalifornia";

    private DBType dbType = DBType.MYSQL;

    private Connection conn = null;

    private ConnectionManager() { }

    public static ConnectionManager getInstance() {
        if (instance == null) {
            instance = new ConnectionManager();
        }
        return instance;
    }

    public void setDBType(DBType dbType) {
        this.dbType = dbType;
    }

    private boolean openConnection() {
        try {
            switch (dbType) {
            case MYSQL:
                conn = DriverManager.getConnection(M_CONN_STRING, USERNAME, PASSWORD);
                return true;

            case HSQLDB:
                conn = DriverManager.getConnection(H_CONN_STRING, USERNAME, PASSWORD);
                return true;

            default: 
                return false;
            }
        }
        catch (SQLException e) {
            System.err.println(e);
            return false;
        }
    }

    public Connection getConnection() {
        if (conn == null) {
            if (openConnection()) {
                System.out.println("Connection opened");
                return conn;
            } else {
                return null;
            }
        }
        return conn;
    }

    public void close() {
        System.out.println("Closing connection");
        try {
            conn.close();
            conn = null;
        } catch (Exception e) { }
    }
}

Upvotes: 0

Views: 205

Answers (3)

user784540
user784540

Reputation:

There is the singleton design pattern.

It used to make sure that only one instance of a class can be created.

public class MySingletonClass {

    private static MySingletonClass instance;


    public synchronized static MySingletonClass getInstance() {
        if (instance == null) {
            instance = new MySingletonClass(); // "lazy" initialization
        }

        return instance;
    }


   /**
    * private constructor can be called only inside of MySingleton class, but not from    outside.
   */
   private MySingletonClass() {
       // your code here
   }
}

So, to get an instance of this class in the code, a developer does not use the constructor.

Developer uses the static method getInstance().

MySingletonClass mySingleton = MySingletonClass.getInstance();

Please be careful with singletons. Many novice developers abuse use of singletons and use them as global variables. Don't do it :)

UPDATE:

I added synchronized to the getInstance() method to make it thread safe.

Upvotes: 2

bemug
bemug

Reputation: 1754

It's called the Singleton pattern.

This is used when you need only one object of a class, the singleton. It will be construct only one time and then you can access it through getInstance().

Naive implementation

public class SingletonDemo {
    //Holds the singleton
    private static SingletonDemo instance = null;

    //Overrides default constructor, not to instantiate another one.
    //Only getInstance will construct
    private SingletonDemo() { }

    //Only this method can construct a singleton, always call this one
    public static SingletonDemo getInstance() {
        if (instance == null) { //No singleton yet, create one
            instance = new SingletonDemo();
        }
        //return the singleton (created this time or not)
        return instance;
    }
}

Upvotes: 1

icza
icza

Reputation: 417572

It simply declares a field called instance whose type is ConnectionManager and initializes it to null (which is redundant because that would be its default value anyway).

Most likely the class is a singleton class (only one instance is allowed from them) judging by the instance field declaration and by the name of the class.

Upvotes: 1

Related Questions