aspose file tools*
The moose likes Java in General and the fly likes private static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "private static" Watch "private static" New topic
Author

private static

Vinney Shanmugam
Ranch Hand

Joined: Aug 27, 2008
Posts: 104
can a instance variable be declared as private static? It's possbile, but once you declare a variable as static, it's no more a instance variable, so I am not getting the ratinale behind it.

Also, What is the use of such a variable, real use case will help me to understand it easily.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Vinney Shanmugam wrote:can a instance variable be declared as private static?
No. An instance variable is a non-static variable (and conversely a static variable is not an instance variable) so you can not have a 'static' instance variable of any visibility.

It's possbile, but once you declare a variable as static, it's no more a instance variable, so I am not getting the ratinale behind it.
You can have a private static variable, yes. This means the variable would be statically available to the class it is declared in. All instances of the class would share the same value, but no other classes could access it.

Also, What is the use of such a variable, real use case will help me to understand it easily.
The first thing to come to mind would be an 'instance counter'. Let's say we have a Widget, and we want all Widgets created in the app to have an ID. We could use a running count of Widgets to make sure each one gets an ID:


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private static