Why do some of the example classes in the Swing Tutorials (sorry, I don't remember which ones; I could be wrong, but I know I saw this somewhere) use private methods like this?:
public MyClass{
public MyClass(){
.
.
.
initializeComponents();
.
.
.
}
private void initializeComponents(){
//initializing
}
}
Is that proper convention, or is this?:
public MyClass{
public MyClass(){
.
.
.
//initializing components
//component initialization
.
.
.
}
}
Which style of initialization is better?
I've only seen the example classes that have a large number of components to initialize use the private method, so it seems like it's for readability. Am I right? If that is so, wouldn't using comments also provide a similar level of readability?
so it seems like it's for readability. Am I right?
Yes it is better for readability as well as keeping all components initialization in a method, similar approach is used with WinForm in .Net framework. If only comments are used then imagine your constructor going to 200 lines of code just for component initialization.
If that is so, wouldn't using comments also provide a similar level of readability?
Component initialization is not just constructor call to the component class, usually it involves setting other properties like Height, Width, Position etc. Now imagine if comments and white space are used, then the constructor would be of hundreds of lines of code, just because of component initialization.