In this video you will learn how to use Eclipse to generate Java source code.
I will cover the following topics:
• Generate getter and setter methods
• Generate constructors
• Override methods
• Code template short-cuts
Please subscribe to this channel 🙂
Time – 00:00
Hi, this is Chad (shod) with luv2code.com. Welcome back to another tutorial on Eclipse. In this video, you’ll learn how to use Eclipse to generate Java source code.
Time – 00:11
We’ll cover the following topics. We’ll learn how to generate Getters and Setters. We’ll also learn how to generate Constructors. We’ll also learn how to override methods, and finally we’ll discuss code templates.
Time – 00:25
Okay, so let’s get started. All right, so let’s go and start by creating a simple Java class. I’m going to create a new Java class called Student and I’ll hit Finish. What I’ll do with this class is I’ll define three fields; private string, I’ll define the last name, first name, and also the age. Now that I have the fields defined, I can have Eclipse generate methods for me, the Getter and Setter methods. I can right click, I can choose Source and Generate Getter and Setters.
Time – 01:07
Eclipse will display a list of fields and you can choose the fields that you want for the Getters and Setters. Here, I’m going to Select All. You can also tell Eclipse where to insert them. Here, we’ll have it insert after the Age field and then also the access modifier of Public and then I’ll just hit OK. Our Eclipse will go often do a lot of work for us and generate these Getter and Setter methods. Note here, I have a pair for last name. I have the Getter and Setter for first name and also the Getter and Setter for age.
Time – 01:41
We can also make use of Eclipse to generate constructors for us. In this example, I’m going to move up to the top and I will insert constructors. I will right click, I’ll move to Source, Generate Constructors using Fields. Okay, so it’ll give me a list of fields to initialize. They’re all selected, and I’ll go ahead and hit OK. Note here, it’s going to create a new constructor for me, so this is the new piece of code that was created. A constructor, same name as the class; last name, first name, age, and it assigns the values accordingly.
Time – 02:20
All right. Another thing we can do with Eclipse is that we can use Eclipse to override methods for us or help us override the methods. One of the methods that you’ll commonly override in your Java programming life is overriding the toString method. Again, we’ll just right click, we’ll go to Source and we’ll move to Override/Implement Methods. Eclipse will give you a list of methods that you can override. The one I’m interested in is toString, so highlight it, select the check box, and I’ll hit OK. Eclipse will generate a toString method for me. It’ll automatically add the override annotation and the stub. This portion here I can remove and I can add my own custom toString implementation.
Time – 03:07
Eclipse also has a collection of co-template shortcuts that you can use. You can view these template shortcuts by going to your Preferences window. Under Window, Preferences, and it’ll give you a list of all of these various templates. The way it works effectively is that if you want to make use of a main template, then you’d simply type the letter or the word main and then you enter CTRL+Space and then Eclipse will do co-completion for you and give you this stub of code. All right, so let’s try it out.
Time – 03:44
Now, I’m going to actually make use of that main co-template. I’ll type the word main and then I’ll hit CTRL+Space on my keyboard. It’ll give me a drop-down list and I’ll choose the main method. Eclipse will go through and give me a basic main method, pretty cool.
Time – 04:00
You can also do a similar thing for a System.out.println. Instead of typing out System.out.println long hand, you can type sysout CTRL+Space. I want to give you System.out.println and you can give whatever information you want to do for the println. There is also support for foreach. You can type foreach and then hit CTRL+Space and it’ll give you that item. Now, we have this “for” loop that’s already entered for us, and I can go through and get a list of command-line arguments that are being passed in to this method. Again, I can use my sysout to print out that information, so sysout CTRL+Space and then I can print out temp.
Time – 04:43
Finally, I’ll show you this one bonus shortcut. I entered my System.out.println and it’s not formatted properly, I can just format my code by doing the right click and I can go to Source and I can choose Format. This will line up all the indentations correctly for my program and so it’ll fix that System.out.println that I have that are for temp.
Time – 05:06
Okay, so let’s go ahead and wrap up. In this video, we learned how to use Eclipse to generate Java source code. Please subscribe through our channel to view more videos and clips in Java. Also, visit our website luv2code.com to download Java source code.