The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about static variable... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about static variable..." Watch "question about static variable..." New topic

question about static variable...

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561

it prints: 1 1
connectionCounter is a static variable. So why am I able to modify this variable in a non static method?

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Andres
You can access a static variable from a non-static method but the vice versa is not possible. That is you can not access a non-static variable from a static method.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
ooppss.. i need a break
It is sorta covered in the JavaRanch Style Guide.
subject: question about static variable...
Similar Threads
about static variable question
How tp print Hello World on the screen many times without using any loops!!?
why dont...
Doubt in the output of this program.
Need Explanation of unpredicted output even if methods are synchronized