Results 1 to 11 of 11

Thread: Constructor s

  1. #1
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default Constructor s

    Java Code:
    public class example{
    private int x;
    public example(){
    this(0);
    }
    public example(int x){
    setX(x);
    }
    public void setX(int x){
    If (x>o){
    this.x=x;}
    else{
    This.x=1;}
    }
    }
    Is it OK to call a method in a constructor like this? Sorry if this is messy I uploaded this on my phone.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Constructor s

    Only if the method in question is final or private, in other words it cannot be changed by a subclass.

    This can result in unexpected bugs.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default Re: Constructor s

    Thanks for the reply. What about a case like my example were you may not want a negative integer. For example making a square with -4 area would not make much sense yet without calling the method to setArea in the constructor it could be created. As calling methods in a constructor can allow you to validate the input(of course you could just put extra validation code in the constructor but that would seem inefficient).

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Constructor s

    There's nothing wrong with putting validation code in constructors and throwing an exception if it is incompatible. What
    happens if a constructor expects an object of some type and you pass in a null? Either you will catch it there or it will wreak
    havoc later - possibly in more than one location.

    In some cases, you can document specific actions in the JavaDoc. In your case for example, you can simply state that signs
    will be ignored (by taking absolute value).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Constructor s

    Quote Originally Posted by Galway View Post
    Thanks for the reply. What about a case like my example were you may not want a negative integer. For example making a square with -4 area would not make much sense yet without calling the method to setArea in the constructor it could be created. As calling methods in a constructor can allow you to validate the input(of course you could just put extra validation code in the constructor but that would seem inefficient).
    No, that's fine, though I would throw an InvalidArgumentException rather than change what was input, but as I said, the method must be marked as final.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Constructor s

    Quote Originally Posted by Tolls View Post
    No, that's fine, though I would throw an InvalidArgumentException rather than change what was input, but as I said, the method must be marked as final.
    Yeah. That's probably best.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: Constructor s

    But what's the point of the first constructor? It is always calling the second constructor with the same argument. And x always gets to be 1.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Constructor s

    It's a default value, presumably.
    You'll see constructor chains similar to the following quite a lot:
    Java Code:
    public SomeClass {
        public SomeClass(int someInt, boolean, flag1, boolean flag2) {
            // set the values, and do whatever other logic applies.
        }
        public SomeClass(int someInt, boolean, flag1) {
            this(someInt, flag1, true);
        }
        public SomeClass(int someInt) {
            this(someInt, false);
        }
    }
    Only the last constructor actually needs the logic in it, the rest just provide default values.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: Constructor s

    Yes, I see. What I meant is that he could have shortened the code if he wanted default value to be 1. No need for if-else?

    Java Code:
    public class Example{
            private int x;
    
            public Example(){
                    this(1);
             }
    
             public Example(int x){
                    setX(x);
              }
    
              public void setX(int x){
                    this.x=x;
             }
        
    
            public static void main(String[] args){
                  Example example1 = new Example();
                  Example example2 = new Example(3);
                  System.out.println(example1.x);
                  System.out.println(example2.x);
           }
              
    }
    Last edited by cronnin; 02-02-2017 at 07:39 PM.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Constructor s

    True, he could have said this(1). But he would still need the if/else to enforce the invariant on the argument x in the set() method. He chose to assign the value 1 if x <= 0. A better idea might have been to simply throw an InvalidArgumentException as Tolls suggested.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: Constructor s

    Oh, my mistake, totally didn't see that. Don't know why :)

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 10:50 AM
  2. Java - Constructor Method versus Constructor
    By brocksoffice in forum New To Java
    Replies: 1
    Last Post: 08-01-2012, 10:17 AM
  3. Replies: 5
    Last Post: 12-14-2011, 02:47 PM
  4. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 10:01 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
  •