Are the setter methods automatically added?
Are the setter methods automatic开发者_StackOverflowally added by Java? If I write getAttributeName()
method, is the setAttributeName()
method automatically working for that class?
No. You have to generate it or write it yourself.
Something like this
No, you have to write both the setter and the getter. Or have your IDE generate them for you.
No, you would have to manually add setter. for example,
private String _name;
public String getName()
{
return _name;
}
public void setName(String name)
{
_name = name;
}
No, You need to write the setter method too.
Nope, you have to add it manually. Well, some IDE's provide a shortcut to make the formats for the setter and getter methods.
If you use Eclipse, you can use the "Generate Getters and Setters" function (I think it was in the Source menu) It'll ask you for which member you need a getter or setter, then it should detect a getter is already there and ask you if you want to generate a setter.
No, setter and accessor methods for classes are just a formality you can use to control what can be done with the class's fields. There's nothing in Java that properly defines them, so Java does not automatically create these methods for you.
精彩评论