Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to get Java eclipse extention\shourtcut for getters setters generation?

So I started to write a POJO class, created public variables and now want to get getters and setters for them (folowing Java Naming Conventions)

so I have for example something like

package logic;

import java.util.Set;
import java.util.HashSet;

public class Route {
  private Long id;
  private String name;
  private int number;
  private Set busses = new HashSet();
}

which eclipse extention and in it which shourtcut will create Getters and setters for me to get something like

package logic;

import java.util.Set;
import java.util.HashSet;

public class Route {
  private Long id;
  private String name;
  private int number;
  private Set busses = new HashSet();

  public Route(){
  }
  public void setId(Long id) {
    this.id = id;
  }
  public void setName(String name) {
    this.name = name;
  }
  public void setNumber(int number) {
    this.number = number;
  }
  public void setBusses(Set busses) {
    this.busses = busses;
  }
  public Long getId() {
    return id;
  }
  public String getName() {
    return name;
  }
  public int getNumber() {
    return number;
  }
  public Set getBusses() {
    return busses;
  }
}
like image 280
Rella Avatar asked Oct 12 '10 13:10

Rella


People also ask

Which setting in the Generate getters and setters wizard in Eclipse allows you to position the methods about to be generated?

Generate getters and setters Click Select All to create getters/setters for all fields. Of course you can choose individual fields as required. Change Insertion point to Last Member. This tells Eclipse that you want to put the methods at the bottom of the class.

Where do getters and setters go in Java?

The Java coding convention states that methods (getters and setters are methods) should be after constructors declarations. It just a convention and it exists to make code easier to read in general.


2 Answers

I think this is availble by default using Ctrl + Shift + G ( I may have set this shortcut myself)

Or go to the Source menu, and select the Generate Getters and Setters option.

You can modify the keyboard short cut (and many others) by going to

  1. Window->Preferences
  2. Expand the "General" option
  3. Select the "Keys" option
like image 50
Kevin D Avatar answered Nov 13 '22 13:11

Kevin D


In Eclipse, right click on the source code and choose Source -> Generate Getters and Setters.

This will open a dialog box where you can choose which of the class members you want to generate for. You can also specify either getters or setters only as well as generating Javadoc comments.

I use this all the time, very handy function!

like image 43
Cyntech Avatar answered Nov 13 '22 12:11

Cyntech