Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: private constructor

  1. #1
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default private constructor

    Hi,

    I have several constructor in my class.
    But i cannot use one constructor for doing al the job and use "this(attr1, attr2,...)" in the others.
    Creating a public constructor with all the attributes is not good because some of the attributes must be set according others.
    Is it OK to create a private constructor then so that is can use (this(attr1, attr2,...) in the public ones?

    Java Code:
        public DataPanel(int row, int col) {
            this(row, col, true, true, true);
        }
        
        public DataPanel(int row, int col, boolean horizontal) {
            this(row, col, horizontal, true, true);
        }
        
        public DataPanel(int row, int col, boolean labels, boolean data) {
            this(row, col, true, labels, data);
        }
        
        private DataPanel(int row, int col, boolean horizontal, boolean labels, boolean data) {
            this.row= row;
            this.col= col;
            this.horizontal = horizontal;
            this.labels = labels;
            this.data = data;
            makePanel();
        }
    Kind regards
    Dipke

  2. #2
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: private constructor

    just create a private method, and call it from your public constructors.
    Java Code:
    public DataPanel(int row,int col){
     whatEver(row,col,true,true,true);
    }
    ....
    private void whatEver(......){
    ....
    }

  3. #3
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default Re: private constructor

    Hi,

    Thanks for the answer.

    Kind regards

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: private constructor

    Quote Originally Posted by Dipke View Post
    Is it OK to create a private constructor then so that is can use (this(attr1, attr2,...) in the public ones?
    It most certainly is OK.

    Quote Originally Posted by arieltal View Post
    just create a private method, and call it from your public constructors.
    While that does give the same end result, code involved in the initialization of an instance of a class is better expressed in a constructor.

    That code should only be expressed in a method if there is a perceived need to run the same code post-construction.

    db
    JosAH likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 4
    Last Post: 09-24-2012, 12:08 AM
  2. Replies: 5
    Last Post: 08-11-2012, 09:50 AM
  3. Replies: 7
    Last Post: 06-28-2010, 07:52 AM
  4. Private constructor and super()
    By Basit56 in forum Advanced Java
    Replies: 9
    Last Post: 08-21-2009, 06:19 AM
  5. [SOLVED] Accessing private constructor
    By piyu.sha in forum New To Java
    Replies: 2
    Last Post: 10-06-2008, 05:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •