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 - Java Tutorial
From the course: Java 8 Essential Training
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
-
-
Understanding encapsulation7m 19s
-
Creating and instantiating custom classes4m 51s
-
Creating nested and anonymous classes4m 17s
-
Visualizing class relationships3m 43s
-
Storing data in instance variables6m 2s
-
Declaring instance and static methods3m 59s
-
Using constructor methods4m 56s
-
Using static variables as constants3m 3s
-
Declaring and using enum types5m 17s
-
-
-
-
-