• Post Reply Bookmark Topic Watch Topic
  • New Topic

static initializer  RSS feed

 
Roshan Wankhade
Greenhorn
Posts: 21
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
i came around code as follow



i know about the static initializer and its significance.
But why there is semicolon at end of code block?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roshan Wankhade wrote:But why there is semicolon at end of code block?

You know what? I honestly don't know.

If you've tried it and it compiles, the only thing I can think of is that the compiler doesn't really care if you have an extra ';' (which is, after all, a null statement) in that context.

Winston
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
If you've tried it and it compiles, the only thing I can think of is that the compiler doesn't really care if you have an extra ';' (which is, after all, a null statement) in that context.

Winston


Agreed. It is just a blank statement. And this is legal too...

 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C++ the class declarations can and do end with ; or so and allowing the spurious ending ; is to make C++ programmers more comfortable with Java.

 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since we're on the subject, would any of you be willing to give me a specific example where static initialization blocks would be used?

To quote from The Java Tutorials:

If initialization requires some logic (for example, error handling or a for loop to fill a complex array), simple assignment is inadequate.


So basically it's the same functionality as a static field, but gives us more choices?

I'd like to get a feel for when it should be used.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> So basically it's the same functionality as a static field, but gives us more choices?

For example a simple assignment might not be adequate, because of a checked exception which might be thrown.
A try-catch construct can be put into a static initializer.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Mrozik wrote:Since we're on the subject, would any of you be willing to give me a specific example where static initialization blocks would be used?


These days I use them for populating maps in enums to look up an enum value by some property of that enum:



So basically it's the same functionality as a static field, but gives us more choices?


All you can do in a static variable declaration+initialization is to generate an expression that can be assigned to that variable. If you need to do any more complex logic, or multiple steps, you have to use a static initializer block. And note that they are not really an alternative to a static variable, but rather are often used as a way to perform the initialization for one.

Additionally, if your initialization of your static variable can throw a checked exception, you can't use a simple declare+init line.

 
Roshan Wankhade
Greenhorn
Posts: 21
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply...

It means its just a blank statement.

I was confused because....
1. That code is from Apache Project.
2. In case of anonymous inner classes, semicolon has to be there after anonymous class block otherwise compiler will complains. I wonder there may such kind of functionality.

I had these points while posting the question.

Thanks for the help...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roshan Wankhade wrote:
2. In case of anonymous inner classes, semicolon has to be there after anonymous class block otherwise compiler will complains.


That's not because it's an anonymous class. There's no particular rule about anonymous classes and semicolons. It's because it's a statement, and statements end with semicolons.

For example:



You understand why we need the semicolon after the new Thread() line, right? Because it's a normal Java statement, and statements end with semicolons.

Now, if we replace r with an anonymous inner class, the rules don't change, and there's no special "semicolon after anonymous inner class" rule. It's just the same syntax as above.




 
Roshan Wankhade
Greenhorn
Posts: 21
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
That's not because it's an anonymous class. There's no particular rule about anonymous classes and semicolons. It's because it's a statement, and statements end with semicolons.



Thanks Jeff, now i got it. Thanks.
 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:

These days I use them for populating maps in enums to look up an enum value by some property of that enum:



I apologize for the late reply, was away. I will have to read a bit more on static initializers as I still don't feel them, but thanks for your input. I'd need to put this to use myself.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!