Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

"this" Keyword in Java

  • 19-12-2012 3:29pm
    #1
    Closed Accounts Posts: 3,596 ✭✭✭


    public class Rectangle {
        private int x, y;
        private int width, height;
            
        public Rectangle() {
            this(0, 0, 0, 0);
        }
        public Rectangle(int width, int height) {
            this(0, 0, width, height);
        }
        public Rectangle(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }
        ...
    }
    

    This class contains a set of constructors. Each constructor initializes some or all of the rectangle's member variables. The constructors provide a default value for any member variable whose initial value is not provided by an argument. For example, the no-argument constructor calls the four-argument constructor with four 0 values and the two-argument constructor calls the four-argument constructor with two 0 values. As before, the compiler determines which constructor to call, based on the number and the type of arguments.


    I got this explanation off here I dont quite understand the part in bold, why would you call one constructor from within another ?


Comments

  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]


    threein99 wrote: »
    public class Rectangle {
        private int x, y;
        private int width, height;
            
        public Rectangle() {
            this(0, 0, 0, 0);
        }
        public Rectangle(int width, int height) {
            this(0, 0, width, height);
        }
        public Rectangle(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }
        ...
    }
    

    This class contains a set of constructors. Each constructor initializes some or all of the rectangle's member variables. The constructors provide a default value for any member variable whose initial value is not provided by an argument. For example, the no-argument constructor calls the four-argument constructor with four 0 values and the two-argument constructor calls the four-argument constructor with two 0 values. As before, the compiler determines which constructor to call, based on the number and the type of arguments.


    I got this explanation off here I dont quite understand the part in bold, why would you call one constructor from within another ?

    The default constructor here just initializes them all to 0... and to do that it calls the constructor with 4 arguments and passes four 0 arguments to that method. If you didn't want to do it this way, and you still wanted the default constructor to assign everything to zero you would have to add 4 lines of code like so:
    public Rectangle() {
            this.x = 0;
            this.y = 0;
            this.width = 0;
            this.height = 0;
        }
    
    

    The way it is done in your sample is a more efficient way of doing it - one line vs four.


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    Because an instantiated object of this class requires 2 corners, on being supplied with 2 arguments the sensible thing to do is assume it is the opposite corner to one at the origin and create an object defined with two vertices, the supplied one and one at the origin.

    If this is unclear consider how a Rectangle.area() method might work if the vertices were undefined

    This is quite a common pattern in Java where defaults can be overridden when instantiating an object.


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    [-0-] wrote: »
    The default constructor here just initializes them all to 0... and to do that it calls the constructor with 4 arguments and passes four 0 arguments to that method. If you didn't want to do it this way, and you still wanted the default constructor to assign everything to zero you would have to add 4 lines of code like so:
    
    public Rectangle() {
            this.x = 0;
            this.y = 0;
            this.width = 0;
            this.height = 0;
        }
    
    

    The way it is done in your sample is a more efficient way of doing it - one line vs four.

    So is it just a way of saving you typing out extra lines of code ? can I call more than one contructor with "this" in the same class ?


  • Registered Users, Registered Users 2 Posts: 2,494 ✭✭✭kayos


    you can have as many constructors using the this keyword as you like.

    The reason you have to use the this keyword in your code is the parameter and class members share the same name. Using the this keyword explicitly calls out that you want to set the class member.


Advertisement