- When you write a new class (whether you’re extending an existing class or not) you are writing the definition for a new Object. That means if you write a new class called ‘Foo’ - if you can compile it, then you could instantiate objects of type Foo.
- When you write a new method in a class it will take the general form:
public <return-type> <method-name>( <parameter-list> ){
<method contents>
}
- Example:
public int addTwo(int a, int b){
return a+b;
}
- Example (from Karel J. Robot):
public void turnRight(){ //empty parameter list
turnLeft();
turnLeft();
turnLeft();
}
- Once you write a method in a class, other methods from within the same class can make a call to it. FOR EXAMPLE: in the Karel J. Robot example above, the turnRight() method just calls turnLeft(). Up to this point if we wanted something to turn left we would have to say something.turnLeft().
- When a class makes calls to other methods within the class, in a way it is a self-referential call. That is to say, the method will be called on whatever instance of the class happens to be running the method.
- I don’t want to belabor the point since it’s not really that difficult a concept - but it is a concept that if you don’t get it, you will really be in trouble.
Leave a Reply