Would the following be considered bad practice in Java?
Code :
public class Call {
private int _CallNumber;
public void CallNumber(int value)
{
_CallNumber = value;
}
public int CallNumber()
{
return _CallNumber;
}
}
as opposed to :
Code :
public class Call {
private int _CallNumber;
public int getCallNumber()
{
return _CallNumber;
}
public void setCallNumber(int value)
{
_CallNumber = value;
}
}
I simply prefer my accessors sorted by the field name, as opposed to get, get, get and set, set, set and I am curious as to whether or not the code at the top would be considered bad form in Java programming?
Re: Would the following be considered bad practice in Java?
First bad practice is to post code outside of code tags which makes it very hard to read.
Edit the post, press the Go Advanced button, select the code and press the #icon.
The naming conventions for java have variable names start with lowercase letters.
Method names are mostly verbs, not nouns. CallNumber is a noun, getCallNumber is a verb
Class names are nouns.
Re: Would the following be considered bad practice in Java?
There are a few differences between the first and second, and a few things (and lack thereof) in both demonstrate bad practice (see Norm's post, and there are a few other things I'd considered bad practice). But focusing on your question at hand - standard practice is to follow the get/set methods of variable access. This is the standard of 'javabeans', some libraries depend upon method names designated as get/set, and more importantly its a standard that allows your code to be readable by others. Many IDE's allow you to automatically define the getter/setter methods for variables.