constuctor calling other constructor

Hi,

I was reading as below

For one constructor to invoke another constructor, we use this keyword and pass
arguments within the parenthesis. Ex: this(10), this(10, ‘a’) etc.


I never remember calling one constructor from other constructor.
Which scenarios we need to call like above. what are practical uses of it. Please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CPColinSenior Java ArchitectCommented:
Picture code like this:

public SomeClass
{
   public SomeClass()
   {
      this(10);
   }

   public SomeClass(int size)
   {
      this(size, "a");
   }

   public SomeClass(int size, String label)
   {
      // Use the "size" and "label" values somehow.
   }
}

Open in new window


If you want to specify a specific value for the "size" and "label" parameters, you can call the constructor that takes two parameters. If you're okay with using the default value for "label", you can call the constructor that takes one parameter. If you're okay with using the default values for both "size" and "label", you can call the constructor that takes no parameters.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rrzCommented:
We can find lots of places in Java's source code. Look at
http://www.docjar.com/html/api/java/awt/Rectangle.java.html   
we can see line 185  
public Rectangle() {
    this(0, 0, 0, 0);
}

Open in new window

and other examples in that code( line 196, 225, 239, 249, and 259).  
Look at API for details describing Rectangle class:  
http://docs.oracle.com/javase/8/docs/api/java/awt/Rectangle.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.