Copyright

What is a Constructor in Java? - Definition & Example

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

An object can't be created in Java without a constructor. In this lesson, we will define a Java constructor and look at working code examples of this key building block of object-oriented programming.

A House Needs a Builder

And in Java, an object needs a constructor. In object-oriented programming, a class provides the plans for the object. If you create an Employee class, it doesn't do much just sitting there in code: You need to create a new instance of that class, a new object.

This is how new objects are created:

Employee emp = new Employee();

In order for the previous code to actually work, that is for the new object to be built, a constructor is required. If you do not create a constructor, one will be created for you. This is called the default constructor. It is hidden, but it is there behind the scenes. In the tree example, it would look like this:

public Employee() { }

Note that the name of the constructor MUST match that of the class! Further, the constructor can be declared as restricted or open, using keywords public, protected, or private. Public constructors can be used by other methods in the same program or other programs. Protected constructors are only used within the same program. Private constructors cannot be used outside of the current class.

Finally, the constructor includes some code between the curly brackets; this is standard Java code, and the possibilities are many. We'll show some code examples later.

Now let's look at the Trees class and create our own constructor. We've created a String in the main class, and the constructor sets a value for that variable:

public class Trees {
  String species;
  public Trees() {
   species = "Elm";
  }
}

Examples

Let's take a look at some code examples of constructors in action. We will use a Currency class that would be part of a currency exchange rate program.

Constructor 1: Single Argument

The following example creates a constructor for the Currency class and has one argument, the country code.

public class Currency {
  String countryCode;
  double exchangeRate;
  public Currency(String c) {
   countryCode = c;
   exchangeRate = .149934;
  }
}

Constructor 2: More Arguments

Next, let's add in both variables as parameters, the country code and the exchange rate. In order to avoid confusion when we set the value in the constructor, we can use the this keyword to refer to the same variables. This way (pun intended), we can refer to the same thing without needing to come up with different names, as in the previous example.

public class Currency {
  String countryCode;
  double exchangeRate;
  public Currency(String countryCode, double exchangeRate) {
   this.countryCode = "CDN";
   exchangeRate = 1.00493;
  }
}

Now, here's the cool thing about this constructor: When we create a new instance of the Currency class, we can also send over the country code and exchange rate. For example, we create a new object for the US Dollar and send the code and the rate:

Currency usd = new Currency("USD", 1.00495);

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support