Java Constructor Types: Default and Parameterized - Quiz

Total: 4 questions

1. 

What is a constructor in Java and how does it differ from an ordinary method?

A constructor is a special block of code that runs when an object is created and brings it into a valid initial state. How it differs from a method:

1. Its name must match the class name exactly, while a method can have any valid identifier.

2. It has no return type — not even void; writing void turns it into an ordinary method.

3. It can only be invoked through new, this(...) or super(...), while a method is called by name as many times as you like.

4. It is neither inherited nor overridden; a method is inherited and can be overridden.

5. If you write no constructor, the compiler adds a default one; methods never appear on their own.

6. A constructor cannot be declared static, final, abstract or native — it takes no part in polymorphism and always works on the specific instance being created.

2. 

What is the difference between a default constructor and a no-arg constructor, and who assigns the default values to the fields?

The default constructor is generated by the compiler, and only when the class declares no constructor at all. It takes no parameters, has the same access modifier as the class, and its body consists of a single implicit super() call.

A no-arg constructor is written by the developer and may contain any code. As soon as you declare at least one constructor of your own — with or without parameters — the compiler stops generating its own, and new Box() may stop compiling.

A common misconception is that the default constructor initializes the fields to their default values. In fact zeros, false and null are written by the JVM when memory for the object is allocated, before any constructor code runs. The generated constructor body itself is empty apart from super().

3. 

What is a parameterized constructor in Java, and how does it initialize the object's fields?

A parameterized constructor accepts arguments and lets you create an object already in the state you need, without a chain of setter calls:

public class Box {
    double width;
    double height;
    double depth;

    Box(double w, double h, double d) {
        width = w;
        height = h;
        depth = d;
    }
}

The values passed at the call site, for example new Box(10, 20, 15), land in the constructor parameters w, h, d and get assigned to the fields width, height, depth. That way the object gets its state immediately, instead of being created "empty" and configured afterward through setters.

4. 

What mistakes do developers make most often with constructors, and what does modern Java offer: the private constructor and the compact constructor of a record?

Common mistakes:

1. Turning a constructor into a method by accident: void Box() { ... } compiles, but it is an ordinary method — the object is built by the generated default constructor and your code silently never runs.

2. Forgetting this when the field and the parameter share a name — the field stays at zero.

3. Calling an overridable method from a constructor: the superclass constructor finishes before the subclass fields are initialized, so the overridden method sees zeros and null. Only private, static and final methods are safe to call.

4. Heavy logic inside a constructor (database access, file reading, starting threads) — move it out or into a static factory method.

5. Expecting constructors to be inherited — declare the ones you need in the subclass and delegate through super(...).

A private constructor prevents code outside the class from creating instances: it is used in utility classes full of static methods, in singletons, and when objects are handed out through static factory methods such as Box.ofCube(7).

A record (Java 16+) removes the need to hand-write a constructor, equals() and hashCode(): the compiler generates the canonical constructor, and argument checks go into the compact constructor:

public record Box(double width, double height, double depth) {
    public Box {
        if (width <= 0 || height <= 0 || depth <= 0) {
            throw new IllegalArgumentException("Dimensions must be positive");
        }
    }
}
Page 1 of 1