Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   1. The Eclipse IDE  >   1.3. Tips about Eclipse  >   1.3.2. How to generate getters and setters

1.3.2. How to generate getters and setters
Last updated: 25 January 2013.

Getters and setters are generated by clicking Source -> Generate Getters and Setters. An example follows.

For this tutorial, you can create a package named com.javaperspective.tutorials.practice in which you will create the classes to use as examples in this and the next tutorials. Once you have created the package com.javaperspective.tutorials.practice, create a class named Point that only contains two fields and a toString method as shown below:

picture showing the eclipse IDE 19


To generate getters and setters, just right-click anywhere in the editor, and click Source -> Generate Getters and Setters as shown below:

picture showing the eclipse IDE 20


In the next dialog box, for each private field, select the methods you want to generate and click OK:

picture showing the eclipse IDE 21


The getters and setters are generated as shown below:

package com.javaperspective.tutorials.practice;

public final class Point {

   
private int x;
   
private int y;

   
public String toString(){
         
return ("Point : x = " + x + ", y = " + y);
   
}

   
public int getX() {
         
return x;
   
}

   
public void setX(int x) {
         
this.x = x;
   
}

   
public int getY() {
         
return y;
   
}

   
public void setY(int y) {
         
this.y = y;
   
}

}

If you want to generate a getter and a setter for a single private field, you have to select it, right-click it and click Source -> Generate Getters and Setters.

The next tutorial will show you how to use the Eclipse debugger.


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   1. The Eclipse IDE  >   1.3. Tips about Eclipse  >   1.3.2. How to generate getters and setters
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   1. The Eclipse IDE  >   1.3. Tips about Eclipse  >   1.3.3. How to use the Eclipse debugger

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7