Can you print out “Hello World!” without writing a main method in Java? Think for a while. Yes, you’re right. It can be possible using “static initialization blocks”...
Personally I hate it! :) It is a horrible hack with little or no real word application. I'm not sure that knowing this makes you a better programmer...
I'm fairly sure that just knowing this specific thing doesn't make you a better programmer...but sometimes people push tecnhnology in weird and wonderful ways just for the fun of it, and that's OK, although it's not everyone's cup of tea. Further, often this sort of tinkering leads to cool real-world innovations. Clearly, writing "Hello World" in a crazy way has no practical utility, but people with this sort of attitude to programming might, for example, discover a new technique for writing an internal domain-specific language by bending the language in a different way.
I had to blog http://dzone.com/links/not_saying_hello_world_without_main_method.html While agree that knowing the ins and outs of a language is good, I have learned to be wary in general of 'hacks' and particularly of using them in real code. In this case I see little value beyond an interest in passing.
Static initialization blocks are underrated but important java language feature.
Sometimes static blocks saves lots of code duplication. It may not be accurate for newbee java users
to use the blocks, but for more advenced developers it is a great feature to use when needed.
Comments
ilazarte replied ago:
Hah. good interview question.
jerryji replied ago:
I do hope you are just kidding
jdave replied ago:
Personally I hate it! :) It is a horrible hack with little or no real word application. I'm not sure that knowing this makes you a better programmer...
MattRussell replied ago:
I'm fairly sure that just knowing this specific thing doesn't make you a better programmer...but sometimes people push tecnhnology in weird and wonderful ways just for the fun of it, and that's OK, although it's not everyone's cup of tea. Further, often this sort of tinkering leads to cool real-world innovations. Clearly, writing "Hello World" in a crazy way has no practical utility, but people with this sort of attitude to programming might, for example, discover a new technique for writing an internal domain-specific language by bending the language in a different way.
jdave replied ago:
I had to blog http://dzone.com/links/not_saying_hello_world_without_main_method.html
While agree that knowing the ins and outs of a language is good, I have learned to be wary in general of 'hacks' and particularly of using them in real code. In this case I see little value beyond an interest in passing.
hhasdal replied ago:
Static initialization blocks are underrated but important java language feature.
Sometimes static blocks saves lots of code duplication. It may not be accurate for newbee java users
to use the blocks, but for more advenced developers it is a great feature to use when needed.
rentzias replied ago:
thnx for the static initialization point and hello world implementation trick.. nice post..
,
zynasis replied ago:
so what, big deal
M Easter replied ago:
how did this get 30+ up votes? odd.
Umberto replied ago:
..old news
Voters For This Link (39)
Voters Against This Link (9)