Methods
| |
For example, here is the getSalary method that returns a double. | |
|
Passing Objects to a Method
class Sphere {
double radius; // Radius of a sphere
Sphere() {
}
// Class constructor
Sphere(double theRadius) {
radius = theRadius; // Set the radius
}
}
public class MainClass {
public static void main(String[] arg){
Sphere sp = new Sphere();
aMethod(sp);
}
private static void aMethod(Sphere sp){
System.out.println(sp);
}
Returning From a Method
public class MainClass {
private int aField;
public void aMethod() {
}
public double volume() {
return 50;
}
}
http://www.java2s.com/Tutorial/Java/0100__Class-Definition/Catalog0100__Class-Definition.htm
No comments:
Post a Comment