Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

constructor style?

Thread view: 
Duane Evenson - 15 May 2006 14:59 GMT
A quick question about constructor style:
Which do you prefer, independently built constructors or nested
constructors?

eg.
// Eclipse automatically generated style
AClass() {
}
AClass(String str) {
 this.str = str;
}
AClass(String str, int num) {
 this.str = str;
 this.num = num;
}

or
// "Elements of Java Style" recommended style
AClass(String str, int num) {
 this.string = string;
 this.num = num;
}
AClass(String str) {
 this(str, DEFAULT_NUM);
}
AClass() {
 this("", DEFAULT_NUM);
}
Domagoj Klepac - 15 May 2006 15:05 GMT
>A quick question about constructor style:
>Which do you prefer, independently built constructors or nested
>constructors?

It really depends on the amount of logic in the constructors, but, as
a general preference, nested constructors - no code duplication.

               Domchi

Signature

Ouroboros ltd. - http://www.ouroboros.hr 
Antispam: to reply, remove extra monkey from reply-to address.

Jeffrey Schwab - 15 May 2006 15:12 GMT
>> A quick question about constructor style:
>> Which do you prefer, independently built constructors or nested
>> constructors?
>
> It really depends on the amount of logic in the constructors, but, as
> a general preference, nested constructors - no code duplication.

Ditto.
Duane Evenson - 15 May 2006 15:25 GMT
>>> A quick question about constructor style:
>>> Which do you prefer, independently built constructors or nested
[quoted text clipped - 4 lines]
>
> Ditto.

thanks
Robert Klemme - 15 May 2006 15:26 GMT
>>> A quick question about constructor style:
>>> Which do you prefer, independently built constructors or nested
[quoted text clipped - 4 lines]
>
> Ditto.

+1

    robert
Tobias Schröer - 15 May 2006 15:21 GMT
Duane Evenson schrieb:
> A quick question about constructor style:
> Which do you prefer, independently built constructors or nested
[quoted text clipped - 24 lines]
>   this("", DEFAULT_NUM);
> }

I'd prefer the latter one. Every constructor finally leads to the "most
flexible" one. If you have to change anything, you have to do it only
once and not - as in this example - thrice.

It's the same with methods: normally you would implement
List#add(Object) as

<code>
public void add(Object obj) {
  this.add(this.size(), obj);
}
</code>

and not do the implementation twice for List#add(Object) and
List#add(int, Object), which are technically the same.

Tobi


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.