This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private & static" Watch "private & static" New topic

private & static

Gagan Deep
Ranch Hand

Joined: Aug 23, 2005
Posts: 47
Hi All,

Above code compiles & run fine but if i will remove comments then it gives compile time error. whcih is true we cannot access private variables out side a class.

But why we are able to declare static variables with private access in a class. As we all know private means we can access that variable only with in that class. But static means we can access the that variable withouht having instance of class & outside of the class.

Any pointer why java allows static & private keyword in same declaration.
Any explanation

Henry Wong

Joined: Sep 28, 2004
Posts: 18506

private means that the variable can only be accessed by "test" objects. While it is true that static mean that it can be accessed without an instance, static also means that there is one copy for all "test" objects.

[ January 24, 2006: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Gregory Jones

Joined: Sep 16, 2005
Posts: 9
A place where you will see a private static variable is a common form of the singleton pattern

Gagan Deep
Ranch Hand

Joined: Aug 23, 2005
Posts: 47
Thanks, Got it.

wood burning stoves
subject: private & static
Similar Threads
Sample question
SingleThreadModel question
Thread variables, local or shared
whether private static variable can be accessed out side the class
Variable Scope