Results 1 to 3 of 3
Thread: Java constructor method
- 07-31-2007, 04:25 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 35
- Rep Power
- 0
Java constructor method
Hi, So I got a Java constructor question...suppose I am creating a GUI through a constructor like so:
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:Java Code:public EmailFrame() { // SETS UP THE UI HERE }
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.Java Code:public EmailFrame(String windowTitle) { // SETS UP THE UI HERE + THE CODE FOR THE 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.
- 07-31-2007, 10:12 AM #2
One way is to send a default value on to the next constructor, viz, the constructor with the next higher number of arguments.
Java Code:public EmailFrame() { this(null); } public EmailFrame(String windowTitle) { // SETS UP THE UI HERE + THE CODE FOR THE TITLE }
- 07-31-2007, 02:15 PM #3
Senior Member
- Join Date
- Jul 2007
- Posts
- 135
- Rep Power
- 0
Similar Threads
-
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
Differences between constructor and method
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 08:53 AM -
Help with constructor in java
By mathias in forum New To JavaReplies: 1Last Post: 08-07-2007, 01:00 AM -
The main method in java...
By lenny in forum New To JavaReplies: 1Last Post: 07-31-2007, 06:21 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks