1

Where would you expect to see the following notation:

 this(a,b,0,null)

and what would be the effect of evaluating this expression?

7
  • 1
    Google for "constructor chaining". Commented Mar 3, 2013 at 16:29
  • 2
    Comment about downvotes: while the question does sound like a copy-paste from homework assignment, I think this is a valid question in itself, and it's quite hard to know what to google, if you do not know what it does. Commented Mar 3, 2013 at 16:33
  • It's hard to google, but if you have a homework, you must have had a lesson beforehand, and notes of this lesson or a textbook. And since it's likely one of his first lesson in Java, it's not hard to search the notes about it. Commented Mar 3, 2013 at 16:36
  • And since very experienced Java developpers give wrong answers, its valid to ask. Commented Mar 3, 2013 at 17:08
  • 1
    @AlexWien sure, nitpicking on the terms "overloaded constructor" is gonna help a beginner a lot. Commented Mar 3, 2013 at 17:45

3 Answers 3

4

This can be seen inside an constructor, to call another constructor from the same class. it has to be the first line in the constructor.
This is a so called alternate constructor invocation.

From Java Language Specification

Alternate constructor invocations begin with the keyword this (possibly prefaced with explicit type arguments). They are used to invoke an alternate constructor of the same class.
An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods or inner classes declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.

Sign up to request clarification or add additional context in comments.

Comments

1

That expression would be legal as the first line inside a constructor. It's effect would be to invoke an overloaded constructor with different arguments.

10 Comments

Are you sure that the term "overloaded constructor" is coreect here?
@AlexWien - That's consistent with the terminology used in the Java Language Specification §8.8.8: "Overloading of constructors is identical in behavior to overloading of methods"
i cited the JLS, in my answer they do not call it over loaded constructor, are they?
@AlexWien - In the next section after what you cited, they use the phrase "overloading of constructors". That makes perfectly clear that constructors can be overloaded and seems to legitimize my terminology.
You are wrong, it has not nessessarily to be an overloaded constructor, it can be any constructor from the same class. The overladed constructor, is the next chapter in the JLS (8.8.8), and is not related to alternative constructor invocation.
|
0

Using this() is to call the constructor from within a class; from within that class' constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.