Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invoking one overloaded constructor from another with this()

 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Eduardo YaƱez Parareda
Ranch Hand
Posts: 93
IntelliJ IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this() calls another constructor in the same class...

In the example you wrote, this(Color.Red) will call 'public Mini(Color)' constructor.
 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Patrick de Kruijf
your code will not work,have you tried it,i can see a compiler error over there
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see three , after including following LOC :-

import java.awt.Color;
 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean to say compiler errors..
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you very much for your effort by adding some code Soniya. It really helped me understanding this().

Thanks again!
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome Patrick! Glad it helped you
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic