• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static methods and static blocks

 
TimD Moore
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic