File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Can we have private static variables ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can we have private static variables ?" Watch "Can we have private static variables ?" New topic
Author

Can we have private static variables ?

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
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

Joined: Aug 16, 2005
Posts: 14338
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
A Thakur
Greenhorn

Joined: Apr 06, 2010
Posts: 20
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.


SCJP 5.0
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

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

Joined: Apr 06, 2010
Posts: 20
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

Joined: Aug 16, 2005
Posts: 14338
    
  22

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

Joined: Feb 12, 2010
Posts: 266
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

Joined: Aug 16, 2005
Posts: 14338
    
  22

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we have private static variables ?