If a class implements a singleton pattern, should all the variables be declared static?
Is there any reason they shouldn t be declared static? Does it make a difference?
If a class implements a singleton pattern, should all the variables be declared static?
Is there any reason they shouldn t be declared static? Does it make a difference?
No. The singleton pattern just means that a single instance is the only instance -- it does not mean "make everything statically accessible".
The singleton pattern gives you all the benefits of a "single instance", without sacrificing the ability to test and refactor your code.
Edit:
The point I m trying to make is that there is a difference between how functionality should be consumed (which depends on context), and how functionality should be initialized.
It may be appropriate that in most cases your object will only ever have a single instance (for example, in your final production system). But there are also other contexts (like testing) that are made much more difficult if you force it to be the only choice.
Also, making something static has more significant implications than just "only one instance of my class should be accessible" -- which is usually the intention.
Further, in software I ve worked on, the initialization and lifecycle of objects is often controlled by someone else (I m talking about DI here) -- and making something static really doesn t help here.
In one common singleton pattern, you do not use statics. You code the class to use ordinary fields, you initialize in the constructor, and then you arrange to execute new MyClass()
once, storing the results in some static place.
No, the only thing that is usually static is the reference to the singleton itself (and there are other ways to store that reference, too, such as JNDI or dependency injection containers).
The reason for not declaring fields as static (even though in a singleton pattern you will need only one instance of them) is that this gives you the flexibility to create another, slightly different instance of the normally singleton class. You may want to do that in special situations, such as for testing.
Even if you do not (think you) need that flexibility, there is no reason to give it up. Declaring a field as static has no benefits that you would lose.
You can do this (not necessarily should). But, even for a singleton, I tend to make all the variables object-level rather than class-level because:
Bottom line: I ve never been able to think of a disadvantage to having them as object-level so that s how I do it. The above two disadvantages to class-level may be minuscule but they re there. It probably comes down to personal preference in the end.
You can read up on how (one possible way) to create a singleton in Java here:
Wikibooks Design Patterns: Java Singleton
Basically you don t need (nor should) make all things in the class static just because you intend to use something as a singleton. There are several reasons
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...