Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 05:25 AM
Member
 
Join Date: Jul 2007
Posts: 35
carl is on a distinguished road
Java constructor method
Hi, So I got a Java constructor question...suppose I am creating a GUI through a constructor like so:
Code:
public EmailFrame() { // SETS UP THE UI HERE }
Now I am required to create a similar GUI but with certain fields different...like the window title of the frame for example and so I create a constructor like so:
Code:
public EmailFrame(String windowTitle) { // SETS UP THE UI HERE + THE CODE FOR THE TITLE
So now we have a bad programming situation here. I have a default constructor that does not take any arguments but the code is similar to the other constructor that does take an argument and everything is the exact same other than the one line that deals with the window title.

How do I go about so that I don't have to re-call the code of the first constructor with minimal work? I have some ideas but before I try I would like to receive some feedback.
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-31-2007, 11:12 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
One way is to send a default value on to the next constructor, viz, the constructor with the next higher number of arguments.
Code:
public EmailFrame() { this(null); } public EmailFrame(String windowTitle) { // SETS UP THE UI HERE + THE CODE FOR THE TITLE }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-31-2007, 03:15 PM
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
I'd do it a little different
Code:
public EmailFrame() { this("Default window title"); } public EmailFrame(String windowTitle) { // SETS UP THE UI HERE + THE CODE FOR THE TITLE }
The other viable option is to put the GUI setup in a private init method and then both constructors call it.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling constructor of parent class from a constructor Java Tip Java Tips 0 12-19-2007 10:10 AM
Calling constructor of same class from a constructor Java Tip Java Tips 0 12-19-2007 10:01 AM
Differences between constructor and method Java Tip Java Tips 0 12-19-2007 09:53 AM
Help with constructor in java mathias New To Java 1 08-07-2007 02:00 AM
The main method in java... lenny New To Java 1 07-31-2007 07:21 AM


All times are GMT +3. The time now is 08:50 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org