Constructors


Invoking a constructor serves as a request to the JVM to construct (instantiate) a brand-new object at run time by allocating enough program memory to house the object’s attributes.

Default Constructors

public class Student {}

Student s = new Student();

Writing Our Own Explicit Constructors

  • Replace default parameterless constructor
  • Overload constructor
  • Using the “this” Keyword to Facilitate Constructor Reuse

There is one very important caveat about default constructors in Java: if we declare any of our own constructors for a class, with any argument signature, then the default parameterless constructor is not automatically provided.

results matching ""

    No results matching ""