Java Questions 31 - 40  «Prev  Next»

Constructors, Boxing, and Fields in Java SE 25

Review constructor overloading and access, superclass initialization, autoboxing, and field defaults in Java SE 25.

  1. Can constructors be overloaded?

    Answer: Yes. A class can declare constructors with distinct parameter signatures, and compile-time overload resolution selects a constructor for a new expression or explicit constructor invocation.

    This is sometimes called compile-time polymorphism, but “constructor overloading” is more precise. Constructors are not overridden and do not participate in virtual method dispatch. Return types cannot distinguish constructor overloads because constructors have no return type.

  2. How does an ordinary constructor invoke superclass construction?

    Answer: It can explicitly invoke a superclass constructor with super(arguments), or delegate to another constructor in its own class with this(arguments). A this chain must eventually reach superclass construction rather than recurse forever.

    If no explicit constructor invocation is present, an ordinary constructor other than Object's implicitly invokes super(). That no-argument invocation must be legal. Java SE 25 permits restricted statements before an explicit invocation; special class forms such as records have additional rules.

  3. What is autoboxing?

    Answer: Boxing converts a primitive value to the corresponding wrapper reference, such as int to Integer. Autoboxing means the compiler applies that conversion in a permitted context, such as Integer count = 3;. Unboxing goes in the opposite direction.

    Unboxing null throws NullPointerException. Wrapper identity is not a reliable substitute for value comparison: use equals or deliberate unboxing rather than == between two wrapper references. Boxing also does not allow every combination of primitive widening and reference conversion in every context.


  4. Which access levels can constructors use?

    Answer: Ordinary class constructors can be public, protected, private, or package-access. Omitting an access modifier means package access. Constructor access determines which code can request that construction path.

    Specialized classes impose restrictions. Enum constructors cannot be public or protected. A record's canonical constructor must meet accessibility rules related to the record declaration. A public class does not necessarily have a public constructor.

  5. What does a private constructor restrict?

    Answer: It prevents ordinary direct construction from code without private access. A class can still expose factories that invoke it internally. Private access is also permitted among appropriately nested classes within the same enclosing top-level class.

    A private constructor alone does not guarantee a singleton or prohibit every reflective mechanism. It is an access-control and API-design choice. Reflection remains subject to its own access and module rules.

  6. What happens to the default constructor when you declare a constructor?

    Answer: For an ordinary class, declaring any constructor suppresses the compiler-provided default constructor. This includes declaring a constructor that takes parameters or a no-argument constructor you write yourself.

    If callers need no-argument construction, declare that overload explicitly and ensure it establishes valid state. A user-written no-argument constructor is not the compiler-generated default constructor.


  7. Why do abstract classes have constructors?

    Answer: Their constructors initialize the abstract superclass portion of an object created through ordinary concrete-subclass construction. An abstract class may hold state and enforce invariants needed by every subclass.

    You cannot directly instantiate the abstract class with new, but a subclass's constructor chain invokes an appropriate superclass constructor. Avoid calling overridable methods during construction, because subclass initialization may not yet be complete.

  8. What if the superclass has no accessible no-argument constructor?

    Answer: A subclass constructor must explicitly invoke an accessible superclass overload, or delegate with this to another constructor that does. It does not need to duplicate the superclass's parameter list.

    public class NamedChild {
        static class Parent {
            private final String name;
            Parent(String name) { this.name = name; }
            String name() { return name; }
        }
        static class Child extends Parent {
            Child() { super("Ada"); }
        }
        public static void main(String[] args) {
            System.out.println(new Child().name());
        }
    }

    This prints Ada. Child has no parameters, while Parent's constructor takes a String. An implicit super() would fail because Parent declares no no-argument constructor.


  9. Which kinds of variables can a class contain or declare in its code?

    Answer: A class can declare instance fields and static fields. Its constructors, methods, and initializer blocks can contain local variables, while methods and constructors can have parameters. These categories differ in scope, lifetime, and initialization requirements.

    Static fields belong to a class identity; instance fields belong to each object. Local variables are not fields and do not receive automatic default values before use. Record components introduce additional declaration rules for record state and accessors.

  10. What is an instance variable, and when does it receive a default value?

    Answer: An instance variable is a non-static field. Each object has its own instance-field variables, including state defined by its superclasses. Defaults are assigned during runtime object creation, not when the source file is compiled.

    Numeric fields start at zero, boolean at false, char at the zero code unit, and reference fields at null. Explicit field initializers and constructors can then establish the intended state. Local variables instead require definite assignment before reading.


References: JLS: constructors and fields, JLS: boxing and unboxing, and JLS: initialization.


SEMrush Software