From the course: Java 11+ Essential Training
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Declare multiple constructor methods - Java Tutorial
From the course: Java 11+ Essential Training
Declare multiple constructor methods
- [Instructor] When you create a new class, you automatically get a no-arguments constructor, but you can override the constructor method and create multiple versions of it. I'll go to the ClothingItem class that I created in a previous exercise. Notice, again, that there is no explicit constructor here. I can generate as many constructors, though, as I need. I'll go to the menu and choose Code, Generate. I'll choose Constructor, and first I'll click on Select None, and that means create a no-arguments constructor. Now, I'm going to move this code up a bit so that it's above the getType method. I'll collapse this code. So I can clearly see that I now have an explicit no-arguments constructor. I don't have to change anything in my main method because the automatically generated version and the explicit version are doing exactly the same thing. I'm still able to create the instance of the class and set and get its values.…