Jtvd78
Jtvd78

Reputation: 4250

Is it reasonable to use a static instance in Java?

I couldn't really find an answer to this question on google, so here goes.

Is it okay to use a Static object so the rest of the program can reference the object? I don't exactly know how to clarify my question, so I'll just show example code.

public class Client {

    Frame f;
    private static Client mainClient;

    public static void main(String[] args){
        new Client().init();
    }

    private void init(){

        mainClient = this;
        f = new Frame();

    }

    public static Client getClient() {
        return mainClient;
    }

    public Frame getFrame(){
        return f;
    }   
}

So, is it acceptable to use the getClient() method throughout the program to get access to the Frame object, as opposed to sending it as a parameter to (most) of the objects I create? Frame is used throughout the program, and adding it as a parameter just adds one parameter to each constructor.

Thanks

Upvotes: 4

Views: 360

Answers (8)

Vlasec
Vlasec

Reputation: 5546

Using static instance is perfectly okay for constants/immutable objects.

As for mutable objects, it depends. In simple programs with one thread, it works well. However, if it's multi-threaded or clustered, you should use singleton with caution or not at all.

Upvotes: 0

darijan
darijan

Reputation: 9785

Well, this implementation of singleton pattern is the one I hate the most. Okay, it's really easy to code it, but later on, if something has to be changed, you would have to change it in million of places. For singleton pattern I would use Dependency Injection - Inversion of Control pattern with a Singleton factory pattern and leave static variables exclusively for constants (e.g. Strings, Integers...).

Regarding your question, everything is acceptable. Implementation depends on your project requirements.

Upvotes: 0

Shark
Shark

Reputation: 6620

Depends on more than one thing...

1) Usage. Do you want to be able to say MyClass.getClient() and get a reference to the Client variable? If you're aiming at a singleton sort of thing - yes. If you're aiming at a very convenient thing - yes if safe, if you just want it visible everywhere - no. If accessing it from wrong place/time causes crashes and bugs - no.

2) People People will use whatever you expose, period. If they see your code fetching Client like that, they will use it when inappropriate as well, so will it cause many bugs? :)

3) Design Do you really need it? Is it cleaner to pass it around like an argument than having absolute access to it at any time?

After gauging those, you decide. This looks like it builds and works fine. But anything that needs this sort of unrestricted access (anytime access mentioned above) to runtime-specifics might not be the best approach; what works for homework might not for enterprise software.

Upvotes: 6

stinepike
stinepike

Reputation: 54692

You just created a variation of SingleTon Pattern. Static object is used when you need only one instance of that object throughout your project.

The basic structure of a singleton is

public class Client {
    private static Client mainClient;


    private void Client (){
         // do initial tasks
    }

    public static Client getClient() {
        if(mainClient == null)
            mainClient = new Client();
        return mainClient;
    }

    public Frame getFrame(){
        return f;
    }   
}

SO you can get the frame using

Client.getClient().getFrame();

Upvotes: 0

André Stannek
André Stannek

Reputation: 7863

What you are describing is called the singleton pattern. Although there are some critics it is an often used pattern.

A second alternative to giving your object to every constructor would be a dependency injection framework. For Java one of the best choices would be Spring.

Upvotes: 1

Alberto Zaccagni
Alberto Zaccagni

Reputation: 31570

To understand if in your case is ok to use the Singleton pattern you should ask "Does that component remain the same during the lifetime of the application?", if yes then probably it would be better to isolate it in a class on its own.

Upvotes: 1

spartygw
spartygw

Reputation: 3452

You're basically asking if it's okay to use a singleton pattern. Sure it is. But the problem is you have to make sure that you don't do GUI things in non-GUI threads. By allowing access to the Frame class you open that up for possible abuse. I assume you are the only programmer so just be careful with how you handle it.

Upvotes: 0

Tala
Tala

Reputation: 8928

It is ok to do that. It would be helpful for you to read about singleton pattern

Upvotes: 0

Related Questions