• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

private static is that make sense ?

 
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
 
author and cow tipper
Posts: 5000
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
Posts: 5000
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.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser 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
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello

Same applies to attributes too ....
 
Marshal
Posts: 69820
278
  • 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
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic