Defining Constructors
In this lesson, we will look at the types of constructors and how they are defined within a Java class.
We'll cover the following
The default constructor
The definition of any constructor begins with the access modifier public
, the name of the class—which is the name of the constructor—and a pair of parentheses. Nothing appears between these parentheses for a default constructor. For example, the definition of the default constructor for the class Greeter
is
/** Creates a default Greeter object. */
public Greeter()
{
greeting = "Hello, world!";
} // End default constructor
Let’s recall how we used the class Greeter
in the first lesson of this chapter:
Get hands-on with 1400+ tech skills courses.