tlauer
tlauer

Reputation: 588

Java - using this

Where would you expect to see the following notation:

 this(a,b,0,null)

and what would be the effect of evaluating this expression?

Upvotes: 1

Views: 83

Answers (3)

AlexWien
AlexWien

Reputation: 28727

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.

Upvotes: 4

christopher
christopher

Reputation: 27346

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

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234807

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.

Upvotes: 1

Related Questions