jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static methods and static blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static methods and static blocks" Watch "Static methods and static blocks" New topic

Static methods and static blocks

TimD Moore

Joined: Aug 19, 2003
Posts: 14
Are non-static variables declared within a static method or static block, of any use by methods outside of that method or block? If not, why would they exist, other than to possibly be used when assigning a value to an actual static variable?
Let me know what you think,
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Any variable declared in any method can only be used in that method (exception of local method classes).
A variable declared in a static block is local to that block the same way that a variable declared in a method is local to that method.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
If you declare a variable within a block, static or otherwise, that variable is still constrained by the scope of that block. Take this example:

In this case, i isn't available anywhere outside the static method. However, it definitely has a use within the static method.
Perhaps I'm missing something in your question.

SCJP Tipline, etc.
I agree. Here's the link: http://aspose.com/file-tools
subject: Static methods and static blocks
It's not a secret anymore!