Sobes.tech
Junior — Middle

Can you list and describe the main constructors used in programming classes?

sobes.tech AI

Answer from AI

In Android (Java/Kotlin), the main types of class constructors are:

  • Default constructor — without parameters, used to create an object with initial values.
  • Parameterized constructor — accepts arguments to initialize the object's fields.
  • Copy constructor (not present as a separate entity in Java/Kotlin, but can be implemented through methods or additional constructors) — creates a new object based on an existing one.

Example in Java:

public class User {
    String name;
    int age;

    // Default constructor
    public User() {
        this.name = "Unknown";
        this.age = 0;
    }

    // Parameterized constructor
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

In Kotlin, constructors can be primary and secondary, allowing flexible object creation.

Can you list and describe the main constructors used… - sobes.tech