P

PrepStack

Constructors and Object Lifetime

A constructor is a special method that sets up a new object. Interviewers ask when the compiler (the tool that turns your code into a program) adds one, how this(...) and super(...) (calls to another constructor) link constructors together, in what order an object is set up, and what happens when an object is no longer needed. Java has no destructor (code that runs when an object ends), so the last answer differs from C++, another programming language.

After this section you can write overloaded constructors (several with the same name but different inputs), predict the printed order of a setup, and explain garbage collection in a few sentences.

Topics in this section