Constructors in Java

In Java there is a better way to achieve the rememberNeighbor of NeighborTalker. We have omitted it from the main text for simplicity.

Java classes can have one or more constructors. These are actually procedures defined within a class that are called when you use the new operator. We have assumed that in the robot language there is only one constructor that takes an int (integer) for each of the street and avenue numbers, an indicator value for the original direction, and an int for the number of beepers (infinity is really just a special int value defined in the underlying robot world).

Java classes can have several constructors, however and giving values to a class's robot references should really be done in a constructor rather than in a rememberNeighbor method. This would prevent a user from creating a robot with the simple constructor and then forgetting to send it the rememberNeighbor message, leaving its neighbor reference null. In this situation the program would halt with an intent error (technically a "NullPointerException" in Java). A better NeighborTalker class would actually look like the following, with a new constructor in place of the rememberNeighbor method. Constructors have the same name as the class and are not marked with a return type like void.

 

public class NeighborTalker extends BeeperPutter
{
	public NeighborTalker(int street, int avenue, Direction direction, int beepers, BeeperPutter aRobot)
	{	super(street, avenue, direction, beepers);
		neighbor = aRobot;
	}
	public void distributeBeepers()
	{	putBeeper();
		move(); 
		neighbor.distributeBeepers();
   }

	private BeeperButter neighbor;
   
}

Note that private features of a class are best put last. This is because a user of the class cannot take advantage of them anyway, but wants to know what services the class provides. These are the public features.

The main task block would then be:


public static void main(String [] args)
{	BeeperPutter LastRobot = new NoNeighbor(1, 5, North, 1);
	BeeperPutter DRobot = new NeighborTalker(1, 4, North, 1, LastRobot);
	BeeperPutter CRobot = new NeighborTalker(1, 3, North, 1, DRobot);
	BeeperPutter BRobot = new NeighborTalker(1, 2, North, 1, CRobot);
	BeeperPutter ARobot = new NeighborTalker(1, 1, North, 1, BRobot);

ARobot.distributeBeepers(); }


Note that we had to rearrange the order of creation so that we created each robot before we made it the neighbor of the next.

The advantage of this is that we cannot now have a partially initialized robot. Every robot created is complete and ready to work. This is an important advantage.

The first statement in a constructor is always a call of another constructor, usually the constructor of the superclass. This is what we have done here in the new constructor. After that you can take care of any special initializations needed for only this sub class. The NoNeighbor class doesn't need a special constructor since it does only the normal initializations. However, it still needs the standard constructor for all robot classes as we have seen before.

public NoNeighbor(int street, int avenue, Direction direction, int beepers)
{	super(street, avenue, direction, beepers);
}