my dog learned polymorphism
The moose likes Beginning Java and the fly likes Static?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static??" Watch "Static??" New topic


Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 202

what does this line of code do? What is the value of x in here?

Be Afraid...Be very Afraid...
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Jun 06, 2002
Posts: 1056
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 ]

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Whoops! Ron is right... I overlooked the int declaration inside the block. My bad.
I agree. Here's the link:
subject: Static??
It's not a secret anymore!