7. Generating code
For example Eclipse can override methods from superclasses and generate the
toString()
, hashcode()
andequals()
methods. It can also generate getter and setter methods for attributes of your Java class.You can find these options in the Source menu.
To test the source generation, create the following class in your
de.vogella.eclipse.ide.first
project.
package de.vogella.eclipse.ide.first;
public class Person {
private String firstName;
private String lastName;
}
Select → , mark both fields and press "Ok".
Select → , select again both your fields and press the "Ok" button.
Select → , mark again both fields and press "Ok".
You created the following class:
package de.vogella.eclipse.ide.first;
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName
+ "]";
}
}