File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static??

 
Paulo Aquino
Ranch Hand
Posts: 202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what does this line of code do? What is the value of x in here?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a static initializer block. See the JLS for the juicy details.
Basically, when the class is being loaded, this static block executes and initializes the static variable x to 5.
 
Ron Newman
Ranch Hand
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does nothing useful. It declares a local variable "x" with an initial value, and then that variable goes immediately out of scope when the code reaches the } at the end of the block.
It doesn't change the real static variable "x" at all.
If it were up to me, I'd have the compiler issue a warning when it saw something like this.
[ September 04, 2002: Message edited by: Ron Newman ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops! Ron is right... I overlooked the int declaration inside the block. My bad.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic