Calling a method on a constructor method

user4980063

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.

Lawrence Aiello

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a void Method into a constructor

From Java

Calling method from constructor

From Dev

Calling a constructor as a method

From Dev

Calling virtual method from a constructor

From Javascript

Calling a class method from the constructor

From Java

Calling an overridden method from a constructor

From Dev

Calling a javascript constructor functions method

From Java

Calling a Method from Constructor Method - Java

From Dev

Calling a constructor from method within the same class

From Dev

Calling a class's method as default arg in constructor

From Dev

Calling a constructor in a int method C++

From Java

Calling virtual method in base class constructor

From Dev

Unity - Inject an object to Constructor by calling a Method of a Class

From Dev

Safe alternative to calling of abstract method from constructor

From Dev

Reflected Method calling constructor multiple times

From Dev

Calling an overridable method in constructor is bad. Are there exceptions?

From Dev

Calling a method AFTER the last constructor ends

From Dev

Calling constructor from a subclass method with a generic parameter

From Dev

Calling an object in constructor in other method/function

From Java

Is this a method or a constructor?

From Dev

Calling a method inside a method

From Dev

calling a method in IBAction method

From Dev

Calling a method in a method

From Dev

Calling action from constructor vs a life cycle method

From Java

Is method reference calling constructor of ArrayNode class under the hood?

From Dev

React Native Issue - Initialize state by calling another method from constructor

From Dev

Calling Dispose method inside constructor which throws an exception or behaves unexpectedly

From Dev

calling an overriden method inside a constructor of both the parent and sub classes

From Dev

Calling constructor of parent class in static method of child class

Related Related

  1. 1

    Calling a void Method into a constructor

  2. 2

    Calling method from constructor

  3. 3

    Calling a constructor as a method

  4. 4

    Calling virtual method from a constructor

  5. 5

    Calling a class method from the constructor

  6. 6

    Calling an overridden method from a constructor

  7. 7

    Calling a javascript constructor functions method

  8. 8

    Calling a Method from Constructor Method - Java

  9. 9

    Calling a constructor from method within the same class

  10. 10

    Calling a class's method as default arg in constructor

  11. 11

    Calling a constructor in a int method C++

  12. 12

    Calling virtual method in base class constructor

  13. 13

    Unity - Inject an object to Constructor by calling a Method of a Class

  14. 14

    Safe alternative to calling of abstract method from constructor

  15. 15

    Reflected Method calling constructor multiple times

  16. 16

    Calling an overridable method in constructor is bad. Are there exceptions?

  17. 17

    Calling a method AFTER the last constructor ends

  18. 18

    Calling constructor from a subclass method with a generic parameter

  19. 19

    Calling an object in constructor in other method/function

  20. 20

    Is this a method or a constructor?

  21. 21

    Calling a method inside a method

  22. 22

    calling a method in IBAction method

  23. 23

    Calling a method in a method

  24. 24

    Calling action from constructor vs a life cycle method

  25. 25

    Is method reference calling constructor of ArrayNode class under the hood?

  26. 26

    React Native Issue - Initialize state by calling another method from constructor

  27. 27

    Calling Dispose method inside constructor which throws an exception or behaves unexpectedly

  28. 28

    calling an overriden method inside a constructor of both the parent and sub classes

  29. 29

    Calling constructor of parent class in static method of child class

HotTag

Archive