Santosh Kumar Nayak wrote:Which is more efficient static block or static method ?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Santosh Kumar Nayak wrote:I faced this as a Interview Question hence I wanted to have a clear vision on the same.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
satisfies your *professional curiosity* (which seems to be lacking in this thread based on many of the other replies).
Luke Leber wrote:Academic questions like this are good opportunities to get to know your tools better. I like to know how things work under the hood before blindly using features, myself.
This small test was conducted using JDK 1.7.0_40 using the javap utility included in the JDK.
While simple cases like this seem fairly dumb, imagine a potentially long-running static initialization process (IE constructing a giant lookup table).
Having both methods available let you determine when to take the time to initialize (possibly in a background thread to shorten application start-up times).
1) Public API - before writing any code 'that works', be sure your public interface is well designed, otherwise using the solution will be painful for others using your work
3) Before optimizing, try an ahead of time compiler (if the situation allows for it) just to check whether or not it'll boost you enough to even need to optimize
A good question! Hopefully this little test gives you a little more insight and satisfies your *professional curiosity* (which seems to be lacking in this thread based on many of the other replies).
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Chan Ag wrote:
Oh another one - 'what is the difference between the equals and the hashCode method?' Seriously. You won't believe it was really a sought after organization ( my personal opinion ) to work for.
Chan Ag wrote:And what's with 'who know some core Java and those who have be asked to tinker with a few web technologies for a year or two'? May be I am one of those cause I can only relate core java things to equals and hashCode. They behave differently if you're working on web technologies?
Chan Ag wrote:I have a question, not with your points, but with such interview questions. If the question is just which of the two is better, do you think somebody should think so much about lazy and eager initializations and answer the question.
Aren't they very specific contexts ( and we must also not forget the class hierarchy implications here - in a way aren't we changing the design when we change a static block to call a static method from the static block.
I remember reading about the static blocks in JLS. Isn't it correct that in many cases, this change could make the code behave differently, more so in multi-threaded environments? ).
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
I also don't see any evidence from your findings that shows me that either technique would differ by anything more than nanoseconds
Winston Gutkowski wrote:
1) Public API - before writing any code 'that works', be sure your public interface is well designed, otherwise using the solution will be painful for others using your work
And on this point, there is something else to remember: You can't easily document a static block.
Winston Gutkowski wrote:
Luke Leber wrote:3) Before optimizing, try an ahead of time compiler (if the situation allows for it) just to check whether or not it'll boost you enough to even need to optimize
And there we disagree. Assuming that your code is correct and well-designed, you should never optimize unless there is a provable need to do so. All the other things you mention only come into play after that condition has been met; and I'm afraid that nothing you've shown us would lead me to believe that there is any condition under which you would choose one of the stated methods over the other for reasons of speed.
Luke Leber wrote:I still think that the correct answer to this question is not that no comparison can be made between apples and oranges, but rather that there is no real performance difference between static initializer blocks and static initialization methods (provided each run exactly 1 time using similar system resources).
Luke Leber wrote:I've re-read this topic twice now without finding a concrete answer and simply provided one with hard evidence to back it up. To me, "Don't worry about it" is not a valid answer.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Don't get me started about those stupid light bulbs. |