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 Static methods and static blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static methods and static blocks" Watch "Static methods and static blocks" New topic
Author

Static methods and static blocks

TimD Moore
Greenhorn

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,
Thanks,
TM
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