Class skeleton

All methods in Java "live in" a class, although one class may contain many methods. If you use BlueJ's "New Class" button, you'll get something that looks like this:

/**
 * Write a description of class MyClass here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyClass
{
    // instance variables - replace the example below with your own
    private int x;

    /**
     * Constructor for objects of class MyClass
     */
    public MyClass()
    {
        // initialise instance variables
        x = 0;
    }

    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    public int sampleMethod(int y)
    {
        // put your code here
        return x + y;
    }
}
Of this, the first few lines are comments about the class: you should fill these in with your name, the date, and the purpose of the class (if any). You'll almost always want a constructor; the most common kind takes in one parameter for each instance variable and sets each instance variable to the corresponding parameter. Some people use parameter names that are exactly the same as the instance variable names:
public class Posn {
   private double x, y;
   public Posn (double x, double y) {
      this.x = x;
      this.y = y;
     }
   // other methods go here
   }
while others use names suggestive of the instance variable:
public class Posn {
   private double x, y;
   public Posn (double xParam, double yParam) {
      this.x = xParam;
      this.y = yParam;
     }
   // other methods go here
   }


Last modified: Fri Feb 20 09:54:22 EST 2009
Stephen Bloch / sbloch@adelphi.edu