Is there a standard acceptable convention for parameters in Java to straightforward constructors and setters?
(I've seen the answer for C++, but practices are often different between the two communities)
Suppose that I have a class C with a foo field.
I have commonly seen the following three options:
public C(Type foo_)
{
foo = foo_;
}
public void setFoo(Type foo_)
{
foo = foo_;
}
public C(Type foo)
{
this.foo = foo;
}
public void setFoo(Type foo)
{
this.foo = foo;
}
public C(Type bar)
{
this.foo = bar;
}
public void setFoo(Type bar)
{
this.foo = bar;
}
I tend to use 2, but I'm wondering what's correct practice.
Option two is most common. In Java it's considered poor practice to use meaningless name prefixes or suffixes to distinguish instance variables from parameters from local variables. But there are no conventions for the names themselves. Use whatever names make the code easiest to understand.