Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > constructor style?

Reply
Thread Tools

constructor style?

 
 
Duane Evenson
Guest
Posts: n/a
 
      05-15-2006
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);
}



 
Reply With Quote
 
 
 
 
Domagoj Klepac
Guest
Posts: n/a
 
      05-15-2006
On Mon, 15 May 2006 13:59:59 GMT, Duane Evenson
<> wrote:
>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

--
Ouroboros ltd. - http://www.ouroboros.hr
Antispam: to reply, remove extra monkey from reply-to address.
 
Reply With Quote
 
 
 
 
Jeffrey Schwab
Guest
Posts: n/a
 
      05-15-2006
Domagoj Klepac wrote:
> On Mon, 15 May 2006 13:59:59 GMT, Duane Evenson
> <> wrote:
>> 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.
 
Reply With Quote
 
=?ISO-8859-1?Q?Tobias_Schr=F6er?=
Guest
Posts: n/a
 
      05-15-2006
Duane Evenson schrieb:
> 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);
> }


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
 
Reply With Quote
 
Duane Evenson
Guest
Posts: n/a
 
      05-15-2006
On Mon, 15 May 2006 14:12:55 +0000, Jeffrey Schwab wrote:

> Domagoj Klepac wrote:
>> On Mon, 15 May 2006 13:59:59 GMT, Duane Evenson
>> <> wrote:
>>> 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.


thanks

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      05-15-2006
Jeffrey Schwab wrote:
> Domagoj Klepac wrote:
>> On Mon, 15 May 2006 13:59:59 GMT, Duane Evenson
>> <> wrote:
>>> 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.


+1

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM
java like constructor calling constructor lallous C++ 5 01-23-2004 11:52 PM
calling a constructor within a constructor Brett Irving C++ 3 06-29-2003 10:43 AM
why it's not possible calling constructor from constructor? Giulio C++ 9 06-25-2003 03:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57