Reputation:
I saw this line of code while doing an Android tutorial, and it compiles:
Date date = new GregorianCalendar(year, month, day).getTime();
How can you call the getTime() method on a constructor. To my knowledge the constructor doesn't return anything.
Upvotes: 1
Views: 41
Reputation: 4638
The constructor doesn't return anything in itself, but it results in the creation of a new object, in this case GregorianCalendar
. What's happening in the code above, is you are just using a member method after creation of this object following the construction.
If it helps, your code above would be equivalent to:
GregorianCalendar calendar = new GregorianCalendar(year, month, day);
Date date = calendar.getTime();
Your example just does it in one line instead of two.
Upvotes: 3