Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private static is that make sense ?

 
Nakata kokuyo
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello

Same applies to attributes too ....
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic