Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private & static

 
Gagan Deep
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Gregory Jones
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A place where you will see a private static variable is a common form of the singleton pattern



HTH
Greg
 
Gagan Deep
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Got it.

Thanks
Gagan.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic