AmosYhpz AmosYhpz -4 years ago 96
Java Question

unexpectedly generate getter and setter in eclipse

public class GetterAndSetter {
private int bestScore;
private int cDate;

//use eclipse generate getter and setter
public int getBestScore() {
return bestScore;
}
public void setBestScore(int bestScore) {
this.bestScore = bestScore;
}
public int getcDate() {//why not be getCDate?
return cDate;
}
public void setcDate(int cDate) {//why not be setCDate?
this.cDate = cDate;
}
}


May it will result in reflect failed? Why eclipse will do it, I don't know why?

Answer Source

This is not the problem of eclipse it is the logic of getter and setter.

it is correct, but for a good practice don't capitalize first two letters, you can take a look about that here Java Tip #6 - Don't capitalize first two letters of a bean property name

This is in our java standards. You should not create a java bean property name that begins with a capital letter in the 1st two places. It can lead to confusing results. We had this happen a few times & finally added it to our standards & enforce it in code reviews. One place we saw problems was in struts. The form bean properties are used in the JSP page, but the Struts framework has to use the getter() & setter() to interact with the bean. This mapping happens based on the java bean spec & in certain cases can cause a method not found error if the developer doesn't name the method just right. The java bean spec provides guidelines on how to map between property and the associated getter() & setter().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download