Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Class constructor default values

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Class constructor default values

    Suppose the following class:

    public class Sample() {
        private int xPos, yPos;
        private int xSize = 10;
        private int ySize = 10;
        // - -
        Sample(int xPos, int yPos, int xSize, int ySize) {
             this.xPos = xPos;
             this.yPos = yPos;
             this.xSize = xSize;
             this.ySize = ySize;
        }
        // - -
        Sample(int xPos, yPos) {
             this.xPos = xPos;
             this.yPos = yPos;
             //this.xSize = 10;
             //this.ySize = 10;
        }
        // etc
    }

    ... or should the second constructor be using the commented out lines instead? Or a different way?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Class constructor default values

    Either method will work (I tend to prefer initializing everything in a constructor rather than in the declaration). Also, there are some syntax errors with your code. You don't put parenthesis after the class declaration.

    Here's how I go about handling multiple constructors:

    Have all the initialization code inside the constructor which takes the most parameters.

    All your other constructors should call this first constructor with the parameters it has and the other parameters to be set as default.

    For example:

    public class Sample{
        private int xPos, yPos, xSize, ySize;
     
        // this is the most complex constructor
        Sample(int xPos, int yPos, int xSize, int ySize) {
             this.xPos = xPos;
             this.yPos = yPos;
             this.xSize = xSize;
             this.ySize = ySize;
        }
        Sample(int xPos, yPos) {
            // simply call the other constructor with the params given to this constructor and the other parameters set to their default value
             this(xPos, yPos, 10, 10);
        }
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    srs (November 25th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Class constructor default values

    ... syntax errors ...
    lol @ me
    I guess I should actually read things when I press the preview button...

    Thanks for the reply.

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Class constructor default values

    np, that's what the edit button's for

Similar Threads

  1. Replies: 1
    Last Post: December 22nd, 2011, 09:55 AM
  2. question about default of outFile()
    By odine in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: August 18th, 2010, 01:11 PM
  3. Replies: 5
    Last Post: August 5th, 2010, 10:16 PM
  4. Construct a class that implement ActionListener with no constructor
    By striko_514 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 5th, 2010, 04:15 PM
  5. get proxy settings from the default system browser
    By reguapo in forum Java Networking
    Replies: 0
    Last Post: January 15th, 2010, 08:43 AM