File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Invoking one overloaded constructor from another with this() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Invoking one overloaded constructor from another with this()" Watch "Invoking one overloaded constructor from another with this()" New topic
Author

Invoking one overloaded constructor from another with this()

Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Hello there,

Hope you doing all well? I'm reading the Headfirst java book, pg 256. This topic is all on one page and I can't find any info on it in the rest of the book or on the web that helps me. So I hope you can teach me what this() does?

The page says: "Use this() to call a constructor from another overloaded constructor in the same class. The call to this() can be used only in a constructor, and myst be the first statement in a constructor. A constructor can heve a call to super() OR this(), but never both."

There is a code on the page that should clear up things, I made the code, including a class called Car, but I still can't figure out what this() does.

Here is the code from the book:


Here is my class Car, I thought I needed it to clear things up. I know there is not much code in it, but I thought the constructor was the only thing I needed for this to work, and it will be added by the compiler anyway, doesn't it?



------------------8<------------------
please cut here
Eduardo Yañez Parareda
Ranch Hand

Joined: Oct 09, 2008
Posts: 92

this() calls another constructor in the same class...

In the example you wrote, this(Color.Red) will call 'public Mini(Color)' constructor.


http://serfj.sourceforge.net - Simplest Ever REST Framework for Java
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Thanks for your quick answer, but what is the advantage of this()?

When I make an object, the noarg constructor will run and invoke another constructor with arguments you have specified with this()? So you have like a default setting if you don't specify any arguments?

Is that true?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Patrick de Kruijf wrote:
When I make an object, the noarg constructor will run and invoke another constructor with arguments you have specified with this()? So you have like a default setting if you don't specify any arguments?


Yes, that's one common use of it: providing default arguments, as shown here.


[Jess in Action][AskingGoodQuestions]
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

@Patrick de Kruijf
your code will not work,have you tried it,i can see a compiler error over there


SCJP6.0,My blog Ranchers from Delhi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Shanky Sohar wrote:
your code will not work,have you tried it,i can see a compiler error over there


I see two! But I think the intent is clear.
Harnoor Singh
Ranch Hand

Joined: Aug 24, 2010
Posts: 35
I see three , after including following LOC :-

import java.awt.Color;
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Thanks for the replies. I know the code will not compile. I copied the code straight from the book, I couldn't think of a code myself, because I didn't know how to use this().

But I think I understand now, thanks to you all!
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

I mean to say compiler errors..
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
Hi Patrick,

The first line of a constructor call is a call to super() by default i.e. this is inserted for you if you do not specify it by yourself or if you do not specify a constructor at all. However, the first line can also be a call to super(with arguments) or to this(with or without arguments). this() calls another constructor from the same class.

A simple example


Thus, we are assigning some default values here in the first two constructors. The third constructor assigns the values as it gets from the caller. Hope it helps you understand "this".


SCJP 1.5 | SCWCD 5 | SCJP 6.0
[url]http://a2zjava.webs.com[/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
http://soniyaahuja.webs.com
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Thanks you very much for your effort by adding some code Soniya. It really helped me understanding this().

Thanks again!
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
You are welcome Patrick! Glad it helped you
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Invoking one overloaded constructor from another with this()