use of setter and getter methods
Hi,
Following is a code snippet that teaches java coding conventions.
Around the bottom of the snippet, I notice setter and getter methods.
I don't know why I am advised to write setter and getter method. Could
anyone explain what the use of setter and getter methods are.
Thank you.
-------------------------------------------------------------------------------------------------------------------
// Non javadoc comments, authorship, and
// class development history.
package javatech.xxx.yyy.zzz;
import java.io.*;
/** javadoc comments about the class. **/
public class SomeClassName
{
int fInstanceVal = 1;
double fVal = 0;
Integer fInstanceRef;
public SomeClassName {
... constructor code ...
} // ctor - for longer constructors
/** Describe the method. **/
public void methodName (...) {
int x = 5;
some_method (x);
if (test) {
do_something ();
}
else {
...
}
...
try {
xxx ();
}
catch (Exception e) {
handle_it ();
}
} // methodName - for longer methods
/** Longer comment.
* - this asterisk is ignored by javadoc
**/
public void someMethodWithLotsOfParameters (
int param1,
int param2,
etc.
) throws SomeException {
...
} // someMethodWithLotsOfParameters
/** Private method names with underscores. **/
private void some_method (int i) {
...
} // some_method
/** Getter method. **/
double getVal () {
return fVal;
}
/** Setter method. **/
void setVel (double val) {
fVal = val;
}
} // class SomeClassName