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
- What a constructor is, and the default constructor
- Overloaded constructors and chaining
- Setup order for one object
- Constructors are not inherited; a copy constructor
- Calling an overridable method inside a constructor
- Object lifetime and garbage collection