File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private & static" Watch "private & static" New topic
Author

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.
Or
Any explanation

Thanks
Gagan.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

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.

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

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

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



HTH
Greg
Gagan Deep
Ranch Hand

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

Thanks
Gagan.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private & static
 
Similar Threads
Sample question
Variable Scope
SingleThreadModel question
Thread variables, local or shared
whether private static variable can be accessed out side the class