TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Eclipse Tutorials

Code generation and formatting with Eclipse IDE

Eclipse Tutorials

Introduction

In this post, we will look at some of the programming tips with Eclipse IDE like automatic code generation and code formatting. Code formatting allows us to indent the code for better readability of the code. To format the code, select Right-click -> Source -> Format. This option would automatically indent the code.

Code Indentation

Let’s unindent the code, as shown in the below picture:

 

 

Now, you can format the indentation using the option.

 

Code format

 

Using this option, Eclipse removes unwanted spaces and tab space in the code and indents your code. Properly indented code increases the readability of the code.

 

The keyboard shortcut for the same is Ctrl + Shift + F . This shortcut helps you to save time if your are formatting code for several classes at a time.

Code generation

Using Eclipse IDE, you can generate code automatically for some methods in a Java class. You can generate code for setter/getter methods, constructors, hashcode(), equals() and toString() methods.

Getter/Setter

To generate getter and setter methods, select the instance variables in the class and right-click -> Source -> Generate Setters and Getters… option. choose the variables and click on Generate button.

 

Code generation Eclipse

 

Variables selection:

 

 

toString()

toString() method generation:

 

 

Code Listing

The auto-generated code for Book class is shown below. Most of the code in the class is generated by Eclipse IDE. These options reduce the time taken to code in Eclipse.

package com.testingdocs;

public class Book {

 private String author;
 private double price;
 private String title;

 public Book(String author, double price, String title) {
 super();
 this.author = author;
 this.price = price;
 this.title = title;
 }

 public String getAuthor() {
 return author;
 }

 public void setAuthor(String author) {
 this.author = author;
 }

 public double getPrice() {
 return price;
 }

 public void setPrice(double price) {
 this.price = price;
 }

 public String getTitle() {
 return title;
 }

 public void setTitle(String title) {
 this.title = title;
 }

 public static void main(String[] args) {
 // TODO Auto-generated method stub
 Book bookObject = new Book("Sears and Zemansky", 99.99, "University Physics");
 System.out.println(bookObject.toString());
 }

 @Override
 public String toString() {
 return "Book [author=" + author + ", price=" + price + ", title=" + title + "]";
 }

}

 

 

Eclipse Tutorials on this website can be found at:
https://www.testingdocs.com/eclipse-tutorials

For more details on the Eclipse IDE, visit the official website at:
https://www.eclipse.org

Related Posts

Download Eclipse Installer

Eclipse Tutorials /

Install Eclipse using Installer on Windows 10

Eclipse Tutorials /

Eclipse Che Overview

Add Eclipse SpotBugs Install URL

Eclipse Tutorials /

Install SpotBugs Eclipse Plugin

Eclipse File Import

Eclipse Tutorials /

Import Maven Project in Eclipse

Eclipse marketplace online catalog

Eclipse Tutorials /

Eclipse Marketplace

‹ Generate Constructors in Eclipse IDE› Eclipse Workspace

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version