Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
Whenever you define your own custom class it has a constructor method. A constructor method is a special kind of method that's called when you create an instance of the class, and as I showed earlier if you create a class that doesn't have an explicit constructor method, such as this Olive class, the Java compiler adds a no arguments constructor method for you. You can create your own custom constructor method though if you prefer. I'm working in a version of the project called constructors that in this Chapter's Exercise Files. Now, place the cursor after the field declarations and before the custom method crush.
A constructor method looks like this. You start with the keyword public and then the name of the Method is always the same as the name of the class. Constructor methods can be designed to receive arguments, but if you're going to have any constructor methods and you want to be able to call the constructor method with no arguments then you need to define it explicitly. So I am going to create a constructor method and within the constructor method I'll just output a message to show when the method is being called. I'll output constructor of and then I'll append the value of the object name field.
When you refer to the name field prefix it with the word this, this in Java means the current instance of the current class. So I'll type in this and I'll see a listing of all the custom fields that I've defined and I'll choose name. I'll Save my changes and Run the application, if prompted with the Run As dialog I'll click Java application and click OK and I see constructor of Kalamata each time I create an instance of the class. Now, notice with constructor methods, you don't do any return type.
You don't put in the keyword void, because constructor methods never return a value, so you put in the accessor method which should always be public, and the name of the class, and you're done. Now as with other kinds of methods you can overload a constructor method, that is, you can have more than one version of it, as long as you distinguish them by the numbers or data type of the arguments. I'll move the cursor down below the no arguments constructor method, and this time I'm going to ask Eclipse to generate a constructor method for me that accepts values into the constructor method and passes those values to the fields that I've already defined.
I'll right click in the empty space, then I'll go to the Source menu and I'll select Generate Constructor using fields. Eclipse shows me all the fields that are a part of this class name, flavor, color, and oil. I'm going to create a constructor method that just receives the value of oil and they'll click OK. Here's the result. The constructor method that's been generated receives an argument named oil data type as an integer the same as the field that matches.
Skipping over the next line, the call to the super method, which I'll explain in a moment, the method takes the argument that's been passed in and passes it to the field. this.oil means the field or instance variable and oil by itself means the argument. If you don't put in the keyword this, prefixing the variable name, you're talking about the argument. Now the super method isn't really needed in this situation. Super opening closing parentheses means call the constructor method of the superclass.
We haven't talked about inheritance yet, and I'll described as more closely when we talk about inheritance later on, but this basically means call the constructor method of the class that this class is inherited from. I don't need that bit of code, I'm going to delete it, and I'll close ups some of this whitespace. Now I'll Save the changes to this file and go back to the main application and I'll go to the constructor methods that I'm already calling when I construct each of the olives. The default value for the oilfield in the Olive class is 3, so when I run the application right now, I end up with 9 units of oil, 3 times 3, but because I've created a constructor method for the class, I can now pass in the number of units of oil when I construct the class, when I instantiate it.
So I'll set these values now to 2, 1 and 2. I'll Save the changes and notice that Eclipse says that's fine. There's a constructor method that's looking for integer argument and that's what you've passed in. I'll run the application and this time, I don't see the output telling me that I've constructed the class, but I get 5 units of oil. 2+1+2, let's go back again and look at the class code. So now I have more than one constructor method, one that accepts no arguments and one that accepts an integer, and this is a very common pattern when you're creating your own custom classes.
You can overload your constructor methods can have multiple versions as long as you distinguish them by the number or data types of the arguments, and then it's up to the Java Virtual Machine and the Compiler to figure out which of the constructor methods will be called. I recommend when you're creating your own custom classes that you always create a no arguments constructor, even if it's not going to do anything. That will make it very clear in your code that a no arguments constructor is executing the default action that is nothing. So I'll go back to olivePress.java and I will place the cursor inside the class declaration, make a little bit of whitespace, and I'll create a constructor method for this class as well.
I'll close up the braces to show very clearly that it's an empty, no arguments constructor method. Again, this is not required, but it is recommended, so that when other developers look at your code they can clearly see the no arguments constructor, and see that it's not going to do anything. That's last change I will make to this application, so I'll run the application again, and show that it still works fine.
There are currently no FAQs about Java Essential Training.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.