Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Googol
  • 1 Post By JosAH

Thread: call default constructor inside of parameterized constructor after conditional check

  1. #1
    Googol is offline Banned
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default call default constructor inside of parameterized constructor after conditional check

    So i have a default constructor, and a parameterized constructor. My understanding is that to call a default constructor within another constructor you have to enter this(); on the first line. The issue is that I want to have a boolean check within the parameterized constructor that calls on the default if the boolean fails. How would I accomplish this?

    Java Code:
    class Something
      {
        ...
    
        Something()
        {
          default stuff
        }
        
        Something(double x)
        {
          if (cond())
            use parameters
     
          else
            //this is where i want to call the default constructor, if the conditional fails
        }
        
        private boolean cond()
        {
          return false;
        }
      }

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: call default constructor inside of parameterized constructor after conditional ch

    Wouldn't it makes more sense to have the check outside of the constructor itself? For example:

    Java Code:
    Something sg;
    if(cond())
        sg = new Something(x);
    else
        sg = new Something();
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    Googol is offline Banned
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: call default constructor inside of parameterized constructor after conditional ch

    It does indeed. Thank You
    Last edited by Googol; 08-11-2012 at 06:03 AM.
    awinston likes this.

  4. #4
    Googol is offline Banned
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: call default constructor inside of parameterized constructor after conditional ch

    Would that have to be inside of the main method? As far as method overloading is concerned is there a way to revert to the default constructor?

    What if inside of the parameterized constructor I had that if(cond()) statement, but skipped the following else statement. In other words if the boolean check fails and nothing is constructed, would it just use the default values?
    Last edited by Googol; 08-11-2012 at 06:08 AM.

  5. #5
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: call default constructor inside of parameterized constructor after conditional ch

    It would be in whatever method you want to utilize the object. As for your second question, I'm not sure.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: call default constructor inside of parameterized constructor after conditional ch

    Move the code of your no-arg constructor to anoher method:

    Java Code:
    public Ctor() { initialize(); }
    private void initialize() { ... }
    Now your other constructor can do things like this:

    Java Code:
    publict Ctor(<parameters>) {
       if (<condition>)
          <do something>;
       else
          initialize();
    }
    As you can see, your other constructor effectively calls your no-arg constructor by calling the initialize() method.

    kind regards,

    Jos
    awinston likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 5
    Last Post: 12-14-2011, 02:47 PM
  2. Replies: 3
    Last Post: 11-01-2011, 09:24 AM
  3. Replies: 7
    Last Post: 10-28-2011, 08:26 AM
  4. Default constructor by compiler.
    By manishdivs in forum New To Java
    Replies: 2
    Last Post: 09-19-2011, 06:31 PM
  5. constructor check
    By YiBoog in forum New To Java
    Replies: 7
    Last Post: 11-19-2009, 09:07 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
  •