Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

10 Useful Eclipse Tricks

Eclipse is the integrated development environment (IDE) of choice for Java developers. Eclipse is popular for several reasons. For one thing, it's free. For another, it has plugins for almost any software task you can think of. If you know how to use Eclipse to develop Java programs, you can reuse your Eclipse skills to program in Python, in C++, and in many other languages. You can even find plugins for doing things other than software development.

Source-->Format

Start with code that's very poorly formatted:

     public class Main 
{
         
         public static void main
(String[] 
args) {System.out.
    println("Hello");
    }}

From Eclipse's main menu, select Source→Format. When you do, Eclipse turns your code into nicely formatted code:

public class Main {
 
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

Maybe you don't quite like the way Eclipse formats the code. Do you prefer your open curly brace at the start of the next line? No problem! You can customize the way Eclipse formats code by selecting Window→Preferences→Java→Code Style→Formatter.

In Eclipse, you can assign, reassign, and un-assign hotkeys. By default, the hotkey for Source→Format is Ctrl+Shift+F.

Source-->Organize Imports

You're on a roll, creating code using classes in the Java API. You don't want to break your stride by stopping to type import declarations:

public class Main {
 
  public static void main(String[] args) {
    JFrame frame = new JFrame("Title");
    frame.add(new TextField("Enter your name:"));
    frame.add(new JButton("Click me!"));
    frame.setLayout(new FlowLayout());
    frame.pack();
    frame.setVisible(true);
  }
}

When you select Source→Organize Imports, or you press the Ctrl+Shift+O hotkey combination, Eclipse adds import declarations automatically. Here's what you get:

import java.awt.FlowLayout;
import java.awt.TextField;
 
import javax.swing.JButton;
import javax.swing.JFrame;
 
public class Main {
 
  public static void main(String[] args) {
    JFrame frame = new JFrame("Title");
    frame.add(new TextField("Enter your name:"));
    frame.add(new JButton("Click me!"));
    frame.setLayout(new FlowLayout());
    frame.pack();
    frame.setVisible(true);
  }
}

Source-->Generate Getters and Setters

Start with a few fields, such as name, and id:

public class Customer {
 
  private String name;
  private int id;
 
}

When you select Source→ Generate Getters and Setters, Eclipse adds getter and setter methods automatically.

public class Customer {
 
  private String name;
  private int id;
 
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  
}

Source-->Generate Constructor using Fields

Start with a few fields, such as name, and id:

public class Customer {
 
  private String name;
  private int id;
 
}

When you select Source→Generate Constructor using Fields, Eclipse adds one or more constructors automatically.

public class Customer {
 
  private String name;
  private int id;
  
  public Customer(String name) {
    this.name = name;
  }
  public Customer(int id) {
    this.id = id;
  }
  public Customer(String name, int id) {
    this.name = name;
    this.id = id;
  }
 
}

Source-->Toggle Comment

Start with a program that contains some code that you don't want to execute:

import static java.lang.System.out;
 
public class Main {
 
  public static void main(String[] args) {
    out.println("Welcome to our company!");
    out.println("You'll love our products.");
    out.println("P.S.:");
    out.println("My boss is a jerk.");
  }
}

In Eclipse's editor, select the undesirable code. Then select Source→Toggle Comment, or press the Ctrl+7 hotkey combination. Eclipse automatically turns that code into end-of-line comments:

import static java.lang.System.out;
 
public class Main {
 
  public static void main(String[] args) {
    out.println("Welcome to our company!");
    out.println("You'll love our products.");
//    out.println("P.S.:");
//    out.println("My boss is a jerk.");
  }
}

If you highlight the code again, and select Source→Toggle Comment again, the end-of-line comments turn back into executable code.

Quick Fix

Eclipse's editor displays a yellow warning marker, or a red error marker.

image0.jpg

If you hover over the marker, you see a message describing the problem.

image1.jpg

On Windows, right-click the marker. On a Mac, control-click the marker. When you do, Eclipse displays a context menu. In the context menu, select Quick Fix.

image2.jpg

Eclipse displays a list of options for fixing the problem.

image3.jpg

When you double-click an option, Eclipse makes the appropriate change to your code.

image4.jpg

Content Assist

As you type your code, Eclipse displays popup hints to help you finish the line of code. For example, if you type the class name JOptionPane, followed by a dot, when you type the dot, Eclipse's popup displays the names of methods belonging to JOptionPane class. When you double-click an option (such as the showInputDialog(Object arg 0) option), Eclipse adds the corresponding code to your program.

image5.jpg

Sometimes, when you think hints would be helpful, Eclipse doesn't display a popup containing hints. When this happens, press Ctrl+Space.

Mark Occurrences

In Eclipse's editor, put the cursor on a name in your program (a variable name, a method name, a class's name, or whatever name). Eclipse automatically highlights the occurrences of that name in your code. For example, if your cursor sits on a customerName variable, Eclipse highlights both occurrences of customerName in the welcome method.

image6.jpg

Eclipse doesn't highlight occurrences of customerName in the thank method because the customerName variables in the welcome and thank methods are two different variables. You could change the names only inside the thank method, and the code would work exactly the same way:

import static java.lang.System.out;
 
public class Display {
 
  public void welcome() {
    String customerName = "Joe";
    out.println("Welcome to our company!");
    out.println("You'll love our products.");
    out.println("Welcome back, " + customerName);
  }
  
  public void thank() {
    String joeName = "Joe";
    out.println("Thank you, " + joeName);
  }
}
 

In fact, you change the names inside the thank method in the very next section.

Refactor-->Rename

In Eclipse's editor, put the cursor on a name in your program (a variable name, a method name, a class's name, or whatever name). In Windows, right-click your mouse. On a Mac, control-click your mouse. As a result, Eclipse displays a context menu.

image7.jpg

If it makes sense for you to change the name, the context menu's Refactor option has a Rename sub-option.

image8.jpg

When you select the Rename sub-option, Eclipse positions the cursor inside the name in the editor. As you delete characters and add characters in the name, Eclipse automatically changes all appropriate occurrences of that name. For example, in the figure below, you're deleting customer from (and adding joe to) the customerName variable. Eclipse simultaneously changes both occurrences of that variable inside the thank method.

image9.jpg

Open Declaration

Your project contains several classes, including a Main class and a Customer class. Inside the Main class, you see the statement

Customer cust = new Customer("Joe");

You don't remember the details of the Customer class's constructor, so you want to jump quickly to that constructor in your project's code.

Place the mouse cursor on the word Customer in the Customer("Joe") call. Then press F3. As a result, the Customer class's code appears in the Eclipse editor. Eclipse scrolls the editor to the declaration of the appropriate Customer constructor.

If you don't like memorizing hotkeys (such as F3 for Open Declaration), you can do the following: In Windows, right-click the mouse and select Open Declaration in the resulting context menu. On a Mac, control-click the mouse and select Open Declaration.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.