Basic Shortcuts in Eclipse IDE for Java Developers

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.

  1. Shortcut for System.out.println
    If you have to type System.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:
    System.out.println Shortcut
    Next, with the cursor after sysout, press Ctrl-Space, and System.out.println(); will appear like magic!
    System.out.println Shortcut

  2. Creating a New Class: Generating Constructors
    Consider the following class diagram for Student class:
    Student Class Diagram
    Create a Student class by doing a right-click on your package in the project folder, then selecting New -> Class. Type in Student into the Name field, like in the following image, and click on the Finished button:
    Create New Student Java Class
    You will now see this:
    Student Java Class
    In between the curly brackets, create the fields:
    Student Java Class Fields
    Now, go to Source -> 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.
    Java Generate Constructor Using Fields
    Go ahead and click the OK button. Your constructor is now generated for you:
    Student Java Class Constructor
    * 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.
  3. Creating a New Class: Generating Getters and Setters
    Go to Source -> 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 the mentor field and check the setMentor(String) option). Click the OK button once you’re done.
    Java Class Generate Getters and Setters

    Ta-da! Your getters and setters are automatically generated:
    Student Java Class Getters and Setters

    * Please complete the rest of the class by adding the display() method. You NEED to follow the class diagram!

  4. Got an Error? Eclipse May Be Able to Fix it Automatically!
    Sometimes, you may encounter red squiggly lines while crafting your code. For example:
    Java Red Lines of Error
    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:
    Java Eclipse Error Suggestions

    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):
    Java Eclipse Fix Error

    * 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. 🙂