aspose file tools*
The moose likes Beginning Java and the fly likes Static blocks vs static items Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static blocks vs static items" Watch "Static blocks vs static items" New topic
Author

Static blocks vs static items

Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
Is there any difference between


and



It doesn't seem to matter in the simple code I am writing for my assignment, and I can't think of why it would, but could it ever?
Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
OK - I just figured out one way it would make a difference - If I needed to access hashKeys or hashValues anywhere else, I would need to use the second form. I don't quite understand why, but I am mulling on it....
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
static {} is a static initializer. It executes when the class is first loaded. Within that block you can declare local variables, which is what you're doing in the first form. As soon as execution leaves the block those variables are out of scope. The second form is one where you are declaring static member variables.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
With other words, a static initializer is very much like a method that gets executed when the class is loaded.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static blocks vs static items