From the course: Java 8 Essential Training

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Using constructor methods

Using constructor methods - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Using constructor methods

- When you create an instance of a class, you typically call its constructor method. In this code, in the project Constructors, I'm calling the constructor method three times, creating three instances of the Olive class. But my Olive class doesn't have an explicit constructor method declaration. That's because of a specific compiler rule. If a Java class doesn't have an explicit constructor, the compiler creates a no arguments constructor for you. But the other part of the rule is that if you have a constructor that does receive arguments, then a no arguments constructor won't be generated for you. If you want it, you have to explicitly declare it. You can have as many constructor methods as you want as long as there's signatures, that is, their data types or the number of their arguments differ from each other. So I'll make some changes to my Olive class. I'll start here, right after my declarations of the instance variables or fields. A constructor method starts with the public…

Contents