June 2004
Intermediate to advanced
848 pages
21h 28m
English
You call a constructor to create a new object. A constructor is a special kind of method that you write as part of a class. It looks like an ordinary method, but a constructor magically allocates memory for a new instance of an object, then executes the statements that you write in the constructor body. Your statements typically initialize the fields of the new object.
Like any method, a constructor needs to be given a name. A constructor also needs to indicate that its return type is “an object of the class that this constructor belongs to”. The Java design team chose to roll these two needs together, by adopting the conventions that:
A constructor has the same name as the class it belongs to.
A constructor ...
Read now
Unlock full access