Reputation: 5042
while implementing singleton as:
class MyConnection {
private static MyConnection connection = new MyConnection();
private MyConnection() {
}
public static MyConnection getConnection() {
return connection;
}
}
1)why we give connection
as static?
Is this only due to the fact that getConnection()
is static and we cannot reference non-static in static context or there is any other reason?
2) is it necessary to declare connection
as final?
Upvotes: 2
Views: 1208
Reputation: 3750
A Singleton is a class of which you'll only ever have one instance. In Java the most common way to implement that pattern is to declare a private
constructor to prevent normal initialization and a public static
method to provide the single instance. (but aioobe's enum answer is cool!)
Static methods can be called without an instance of the class, allowing you to do things like MySingleton.getInstance()
- however, since you're not guaranteed to have an instance of the class, there's no way to access the instance variables. By declaring a variable as static
you declare it as a "class variable" - allowing access to it in the same way as you do static methods, and also allowing static methods to access it (again, static methods cannot access non-static variables, because there's no guarantee that they exist and we wouldn't know which instance to look in anyways). So to answer 1) it's because if you needed an instance of the singleton in order to look up the instance of the singleton, you'd have a catch 22.
As for 2) you don't need to declare it final, but it's good form to declare anything that can be final, final. That way you're explicit about your intentions and the compiler can help you if you make a mistake. It should be at least final or private.
Upvotes: 1
Reputation: 15446
1)why we give connection as static?
Because you want it to be singleton. Even though you are not accessing it through static method, you can access the static variable by making it public and final
. But, the variable has to be static.
2) is it necessary to declare connection as final?
Not Required if you are accessing through static method like in your code, as it not exposed outside to get modified.
But if you make the singleton variable public , it has to be final also.
Upvotes: 1
Reputation: 421010
1) why we give
connection
as static?
If it was non-static, you would need to have an instance of MyConnection
to get hold of the connection
reference which sort of defeats the purpose. :)
Is this only due to the fact that
getConnection()
is static and we cannot reference non-static in static context or there is any other reason?
Yes. (Since getConnection()
needs to be static, connection
needs to be static.)
2) is it necessary to declare connection as final?
No, but it's good practice, since once initialized, it should not be changed.
But, an even better practice is to use an enum
instead.
enum MyConnection {
INSTANCE;
// your methods...
}
and access it through MyConnection.INSTANCE
.
Rule of thumb: If a class shall have a predefined number of instances, use an enum
. In this case the number of instances is one.
Upvotes: 9