Is it possible to use a static instance in Java?

I could not find the answer to this question on google, so here.

Can I use a Static object so that the rest of the program can reference the object? I don’t know exactly how to clarify my question, so I’ll just show an 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 access the Frame object, and not send it as a parameter to (most) of the objects that I create? The frame is used throughout the program, and adding it as a parameter simply adds one parameter to each constructor.

thanks

+4
source share
8 answers

Depends on a few things ...

1) Use . Do you want to say MyClass.getClient () and get a link to a client variable? If you are aiming for a singleton thing - yes. If you are aiming for a very convenient thing - yes, if it is safe, if you just want it to be visible everywhere - no. If access to it from the wrong place / time causes failures and errors - no.

2) People People will use everything that you exhibit, period. If they see that your code is being selected by the client this way, they will use it when it is unacceptable, so that will cause a lot of errors? :)

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

Once you evaluate it, you decide. It looks like it builds and works great. But anything that requires this kind of unlimited access (at any time specified above) to runtime specifications may not be the best approach; what works for homework may not be for enterprise software.

+6
source

To understand whether you can use the Singleton template in your case, you should ask: “Does this component remain unchanged throughout the life of the application?”, If so, then it would probably be better to isolate it in the class on your own.

+1
source

What you are describing is called a singleton pattern . Although there are some critics, this is a commonly used pattern.

A second alternative to providing your facility to each constructor would be injection dependency . For Java, one of the best options would be Spring .

+1
source

This is normal. It would be helpful to read about singleton pattern.

0
source

Basically you ask if using a singleton pattern is good. Of course yes. But the problem is , you have to make sure that you are not making a GUI in threads without a GUI . By providing access to the Frame class, you open it for possible abuse. I assume that you are the only programmer, so be careful how you deal with this.

0
source

You have just created the SingleTon Pattern variant. A static object is used when you need only one instance of this object in the entire project.

The basic structure of a singleton

 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(); 
0
source

Well, this singleton pattern implementation is the one that I hate the most. Well, it is very easy to encode it, but later, if you need to change something, you will have to change it in millions of places. For a singleton template, I would use dependency injection - the inversion of the control template with the Singleton factory template and leave static variables exclusively for constants (for example, strings, integers ...).

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

0
source

Using a static instance is great for constants / immutable objects.

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

0
source

Source: https://habr.com/ru/post/1487896/


All Articles