Eclipse IDE for Java Developers is a very popular IDE used by Java developers, as it is free and boasts of many awesome features to enhance the experience of Java programming. I recall back in uni when I was learning Java, I had to do all of my Java programming on Emacs on Linux; everything was very manual.
I’m going to cut to the chase and introduce some very basic shortcuts you may use in Eclipse. My screenshots are taken from the Mars 2 version of the IDE, but it should work similarly for most versions.
PS: This guide assumes you have already created a project folder in Eclipse to contain your Java files.
- Shortcut for
System.out.println
If you have to typeSystem.out.println
very often in your codes to get output in the console, you should use a shortcut in your Eclipse IDE to save some time.First, type
sysout
in your editor:
Next, with the cursor aftersysout
, press Ctrl-Space, andSystem.out.println();
will appear like magic!
- Creating a New Class: Generating Constructors
Consider the following class diagram for Student class:
Create aStudent
class by doing a right-click on your package in the project folder, then selectingNew -> Class
. Type in Student into the Name field, like in the following image, and click on the Finished button:
You will now see this:
In between the curly brackets, create the fields:
Now, go toSource -> Generate Constructor using Fields
and you will see the following window. Note that you may check the “Omit call to default constructor super()“, as this class is not a subclass.
Go ahead and click the OK button. Your constructor is now generated for you:
* Depending on the class diagram and requirements of the program, you may not need to select all the fields. It is very important as programmers and software engineers to follow specifications exactly. - Creating a New Class: Generating Getters and Setters
Go toSource -> Generate Getters and Setters
, and you will see the following window. Select only the necessary getters and setters. In our example, we need all three getters for all the fields (click on the Select Getters button), and only the setter for the mentor field (expand thementor
field and check thesetMentor(String)
option). Click the OK button once you’re done.
Ta-da! Your getters and setters are automatically generated:
* Please complete the rest of the class by adding the
display()
method. You NEED to follow the class diagram! - Got an Error? Eclipse May Be Able to Fix it Automatically!
Sometimes, you may encounter red squiggly lines while crafting your code. For example:
Eclipse is pretty smart with its suggestions sometimes. Clicking on the lightbulb with a red cross at the side will yield some suggestions that can fix your problem very swiftly with just a click of a button:
In this example, the first suggestion is the correct fix to the error. By clicking on Eclipse’s suggestion, it will modify your program to fix the problem (in this case, import the
ArrayList
library):
* Red lines are errors, and when they appear, you will not be able to run your program. Yellow lines are warnings; even though they appear, you will still be able to compile and run your program. Yellow lines usually appear when you haven’t used your variables.
However, please use this method with caution. Not all suggestions by Eclipse is a solution to your error! You still need to know what you are doing.
So there you go! These are some very basic Eclipse features that novice Java programmers may find useful. 🙂