aspose file tools*
The moose likes Java in General and the fly likes  private static is that make sense ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark " private static is that make sense ?" Watch " private static is that make sense ?" New topic
Author

private static is that make sense ?

Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 438
hi, what will happen if our variable make it as private static ?
ie : private static connection conn = null;

is that means "conn" only can access by member of static method in class ?

thanks for guidance
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Static methods and variables: the variables that stick to your clothes when you pull them out of the dryer.

If you make a variable static, every instance of the class uses the same object. So, that Connection of yours is going to get passed around between all of your different instances. Germs will spread, and things might get messy.

Connection pooling. That's where it's really at.

If you have problems with static, just throw in some fabric softener. That always seems to do the trick for me.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

More to your point:

instance variables can only be accessed in instance methods.

instance varialbes cannot be accessed by static methods, unless explicitly passed in as arguments, which doesn't really count.

static variables are accessible in all static AND non-static methods of a class.

non-static methods: non-static and static variables can be used
static methods: only static variables

Think that gets more to the heart of your question.
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hello,

To put straight it is perfectly valid to have private static. But you can access it from static context with in class not from outside of the class or class object.


class kin{
private static void j()
{

}
public static void din()
{
kin.j();
}

}
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

hello

Same applies to attributes too ....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
Posted by Srinivas Kalvala
you can access it from static context with in class
No, Kameron McKenzie is correct. You can access a static member from a static or instance ("non-static") context. If it is private only from inside the class.

Example of its use:
Imagine you have a ListItem classNow you have a private static count field whihc counts how many instances of the class there are, and you can get that information from the public static ListItem.getCount() method.

I hope this has helped you. CR
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private static is that make sense ?