Specifications

Sun Services
Java™ Programming Language
Module 2, slide 10 of 26
Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F
Declaring Methods
Basic syntax of a method:
<modifier>* <return_type> <name>
(
<argument>*
) {
<statement>*
}
Examples:
1 public class Dog {
2 private int weight;
3 public int getWeight() {
4 return weight;
5 }
6 public void setWeight(int newWeight) {
7 if ( newWeight > 0 ) {
8 weight = newWeight;
9}
10 }
11 }