This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have been studying for the SCJP 5 Version and have come across initialization blocks.
I am really sorry to object to this. The java language was a model of elegance a work of art. It was so beautifully constructed. Java was clean while C++ was messy and unintelligence. No matter how badly Java was written you could understand it unlike C++. The language I always work with, C# is based overwhelming on java and is like elegance. Howerever these initialization blocks, code that operates outside the constructor and methods reduces Java's elegance. It is a feature of java that makes it a bit messy like its infamously messy predecessor C++.
MCPD (Enterprise Application Developer, Windows Developer, Web Developer - .NET 2.0), MCTS (Windows Apps, Web Apps and Disbributed Applications - .NET 2.0), MCITP (Database Developer & Business Intelligence Developer - SQL Server 2005), MCAD, MSCD.net, SCJP 5, SCWCD 1.4, SCBCD, SCMAD, SCDJWS, SCJA
Are you referring to (static) initializers? C# has an exact equivalent, only they are called "static constructors". Java is an extremely poorly designed language, and Microsoft learned a few of its mistakes on which to improve (though not many) with C#.
However, I think the use of "static contructors" in place of "static initializers" is potentially ambiguous and confusing. Overall, C# is an improvement, but certainly not for the case of initializers
Originally posted by Dibbo Khan: It is a feature of java that makes it a bit messy like its infamously messy predecessor C++.
This is a funny comparison, considering that static init blocks (and the class loading process they're a part of) are an elegant solution to something that's a horrific problem in C++ -- explicitly because C++ has no equivalent to them. In C++, it's impossible to know the order in which class data will be initialized, and it causes no end of problems. It's actually very easy in C++ to use static members before they're initialized! The fact that Java provides a way to do that initialization, catch and handle any resulting exceptions, and guarantee that all statics will be initialized before their first access, is part of what makes Java [i]very[i] elegant compared to C++.
Originally posted by Dibbo Khan: I have been studying for the SCJP 5 Version and have come across initialization blocks.
I am really sorry to object to this.
Well, I think this could be a more constructive discussion if you told us *what* you think is messy about it.
I don't use (static and non-static) initializers often, but when I use them, it looks rather elegant and easy to understand to me...
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 have never used static initializers, and to be honest I haven't ever really understood the need for them. I understand Ernest's comments above, but can somebody post an example of when such a situation could occur please?
Joined: Jul 11, 2001
Static initializers are good for complex initializations of static fields (for example constants).
Instance initializers are useful from time to time because anonymous inner classes can't have constructors.
Er, yes in that case you are right - I have used them
I guess they would also be needed if we wanted to create an static instance and needed to construct the instance and call several methods on it too, right? Maybe for some strange reason we want to create a static Swing component and then add a listener to it. We would put that code into an initializer because it wouldn't make sense to duplicate the code to add the listener in each constructor?
Not the best example in the world, but am I on the right track?