The Artima Developer Community
Sponsored Link

Java Buzz Forum
Use constructor chaining

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Marc Logemann

Posts: 594
Nickname: loge
Registered: Sep, 2002

Marc Logemann is founder of www.logentis.de a Java consultancy
Use constructor chaining Posted: Nov 5, 2004 6:35 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Marc Logemann.
Original Post: Use constructor chaining
Feed Title: Marc's Java Blog
Feed URL: http://www.logemann.org/day/index_java.xml
Feed Description: Java related topics for all major areas. So you will see J2ME, J2SE and J2EE issues here.
Latest Java Buzz Posts
Latest Java Buzz Posts by Marc Logemann
Latest Posts From Marc's Java Blog

Advertisement

Recently i had the pleasure to review a lot of third party java code and one thing that i realized is that many programmers dont know how to handle components / classes with a lot of constructors. I saw redundany of property assignment and an overall poor approach in defining multi-constructor classes.

I really prefer to do constructor chaining whenever possible, this way, your classes keep the complexity low and the last constructor in you chain is a perfect place to do changes.

Just check out JTextField from Sun to see what i mean:

01 
02 public class JTextField extends JTextComponent implements SwingConstants {
03 
04     public JTextField() {
05         this(null, null, 0);
06     }
07 
08     public JTextField(String text) {
09         this(null, text, 0);
10     }
11 
12     public JTextField(int columns) {
13         this(null, null, columns);
14     }
15 
16     public JTextField(String text, int columns) {
17         this(null, text, columns);
18     }
19 
20     public JTextField(Document doc, String text, int columns) {
21         if (columns < 0) {
22             throw new IllegalArgumentException("columns less than zero.");
23         }
24         visibility = new DefaultBoundedRangeModel();
25         visibility.addChangeListener(new ScrollRepainter());
26         this.columns = columns;
27         if (doc == null) {
28             doc = createDefaultModel();
29         }
30         setDocument(doc);
31         if (text != null) {
32             setText(text);
33         }
34     }
35     [..]
36 }

Its really not that surprising and its in no way new to do it this way, but i just saw too much of weird things happening inside the classes of other programemrs when it comes to multiple constructors.

Read: Use constructor chaining

Topic: Golden Google Previous Topic   Next Topic Topic: The first two laws of Software Engineering

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use