What this topic covers
18 subtopics- Constructors9
- Initialisation order4
- Methods3
- Object equality3
- References3
- Value objects3
- Attributes2
- Basics2
- Class vs instance attributes2
- Constructor chaining2
- Copy constructor2
- Object methods2
Show all 18 subtopics
- Objects in memory2
- Overloading2
- Classes at runtime1
- Design1
- Nested classes1
- State and behaviour1
- Beginner 15
- Intermediate 15
- Advanced 15
About this topic
A class is a blueprint; an object is one instance of it with its own state. Constructors set that state up, and most output puzzles test when each piece of initialisation code runs: field initialisers, initialiser blocks, this(…) chaining, member initialiser lists. Remember that in Java and Python a variable holds a reference to an object, so two variables can point to the same object. Every program here was run to produce its answer.
Free preview
Sample questions
Try each one first, then open the answer for the explanation and why the other options are wrong.
The class declares no constructor. What does this print?
public class Main {
static class Point {
int x;
boolean visible;
String label;
}
public static void main(String[] args) {
Point p = new Point();
System.out.println(p.x + " " + p.visible + " " + p.label);
}
}
Show answer and explanation
Explanation
Without any constructor, Java supplies a default no-argument one, and fields get default values: 0, false and null. (Local variables, by contrast, must be assigned before use.)
Why the other options are wrong
- AJava never exposes uninitialised memory.
- BReference fields default to null.
- CFields are initialised automatically.
A field initialiser, an instance initialiser block and a constructor all print. What does this print?
public class Main {
static int log(String s) { System.out.print(s); return 0; }
static class Widget {
Widget() { log("C"); }
{ log("I"); }
int size = log("F");
}
public static void main(String[] args) {
new Widget();
}
}
Show answer and explanation
Explanation
Field initialisers and instance initialiser blocks run in the order they appear in the source, before the constructor body – so I (written first) then F, then C. The position of the constructor in the source does not matter.
Why the other options are wrong
- AOrder follows the source text: the block comes before the field.
- CInitialisers run before the constructor body.
- DThe constructor body runs last.
Two objects are created. What does this print?
public class Main {
static class Engine {
static { System.out.print("S "); }
{ System.out.print("I "); }
Engine() { System.out.print("C "); }
}
public static void main(String[] args) {
System.out.print("main ");
new Engine();
new Engine();
}
}
Show answer and explanation
Explanation
The static block runs once, when the class is first initialised – here on the first new Engine(), after "main" has printed. Instance blocks and the constructor run for every object.
Why the other options are wrong
- AStatic initialisation comes before any instance code.
- CStatic blocks run only once.
- DEngine is initialised lazily, on first use.