0

When you have many classes, it is often necessary to get access to fields/methods of one class from the other one. I'm not very experienced with Java and I've recently found this method which seem to be very convenient (better than passing variables from one class to another):

class MyClass {
    private static MyClass _instance = null;

    public MyClass(){
        ...
    }

    public static getInstance(){
        if (_instance == null) {
            _instance = new MyClass();
        }
        return _instance;
    }
}

Now in any other class I can simply do

MyClass.getInstance().callSomething()

However I do not see such way of accessing other classes in Java examples and tutorials that I find in the Internet. So that got me thinking, are there maybe some potential problems with such approach? Especially when you have many classes and hence many such static calls in each?

Tiny
  • 27,221
  • 105
  • 339
  • 599
Lez77
  • 147
  • 10
  • this is called a `Singleton` and very much used in Java or any other object oriented language for that matter. You use it when you only want a single instance of a class – Ben Sep 21 '14 at 09:52
  • does **public static getInstance(){}** even valid? – Nabin Sep 21 '14 at 09:55
  • 1
    It's not a Singleton: the constructor is public. And @Nabin is right, it isn't a valid method signature. – Olivier Grégoire Sep 21 '14 at 13:39

2 Answers2

2

MyClass.getInstance().callSomething()

However I do not see such way of accessing other classes in Java examples and tutorials that I find in the Internet.

The approach you have specified is one of the most commonly used design pattern called Singleton pattern.

In Java the Singleton pattern will ensure that there is only one instance of a class is created in the Java Virtual Machine. It is used to provide global point of access to the object. In terms of practical use Singleton patterns are used in logging, caches, thread pools, configuration settings, device driver objects. Design pattern is often used in conjunction with Factory design pattern.

However, your code does not seem to fulfill the contract of Singleton Design pattern. It primarily requires your class to have only private constructors (to avoid other classes from instantiating it).

public MyClass(){ // Constructor needs to be private
        ...
    }

Also the getInstance() method does not even seem to be valid. Try this:

public static MyClass getInstance(){

It is recommended to implement Singleton in Java 5 or above version using Enum. Enum is thread safe and implementation of Singleton through Enum ensures that your singleton will have only one instance even in a multithreaded environment

Follow this link for detailed understanding of Singleton pattern using Enum

Here is a good link which can help you understand Singleton pattern in detail.

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
  • +1 for enums. See also http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java – Constantinos Sep 21 '14 at 10:12
0

This design pattern is called Singleton, have a look at the following question.

What is so bad about singletons?

Community
  • 1
  • 1
Bilbo
  • 168
  • 7