aspose file tools*
The moose likes Java in General and the fly likes Lack of elegance in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Lack of elegance in Java" Watch "Lack of elegance in Java" New topic
Author

Lack of elegance in Java

Dibbo Khan
Ranch Hand

Joined: Dec 19, 2004
Posts: 147
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
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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


Tony Morris
Java Q&A (FAQ, Trivia)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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++.


[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
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?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8708
    
    6

Originally posted by Stuart Gray:
I have never used static initializers,


I bet you have, you just don't realize it. You ever create anything like the following?

There really isn't such a thing as a static array in Java. The compiler creates a static initalizer block and initalizes the array and each value when the class is loaded.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
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?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Yes, you are!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lack of elegance in Java
 
Similar Threads
Static Constructors???
Initialization Blocks
do you recommend C++ for an introductory programming course?
How do I import specific classes ?
Blocks