• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we have private static variables ?

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was asked a question on Singleton and when I replied with he below code I was told that private static variables should not be used although they do not have give any error.

public class MySingleton {
private static MySingleton _instance =
new MySingleton();
private MySingleton() {
// construct object . . .
}
public static MySingleton getInstance() {
return _instance;
}
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing wrong with using private static variables.

Did the person who told you that also explain why you shouldn't use private static variables? If not, then ask him or her.
 
A Thakur
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the code you wrote above is wrong.

Only think I can think of is: you making the _instance private static for a singleton class is redundant. Since there will be only one instance of your class anyhow since you made your class's constructor as private.

And private static for _instance also ensures the same thing. Bottom line is there will not be more than one instance of your class.

Hope it helps.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Art Thak wrote:Only think I can think of is: you making the _instance private static for a singleton class is redundant.

I don't think it is redundant. What would you propose instead, making the member variable public? It's (almost) never a good idea to make member variables public.

I'd normally implement a singleton class like this, with lazy instantiation:
 
A Thakur
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper,

I didnt mean to make it public. But just "private" could also serve your purpose of singleton class instead of "private static".

I guess I was not clear enough

Thanks for that.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Art Thak wrote:I didnt mean to make it public. But just "private" could also serve your purpose of singleton class instead of "private static".

How would you create a singleton class with a variable that holds the instance that is not static?
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for responding,

may be instead of :
private static Singleton instance;

i can use just:
static Singleton instance;
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose: You can, but why would you do that? You don't want any class outside your Singleton class to be able to directly access the member variable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic