next up previous
Next: if Statements Up: The Frightened Freshers Guide Previous: Types

Methods

So, its all well and good being able to define some variables and do stuff in the constructor with them, but what about after the object has been created? Well, for this you need methods. Methods in a Class take the following form:


\begin{lstlisting}
scope returnType helloWorld(parameterDatatype parameterName)
{
// Do shiny things here
}
\end{lstlisting}

This probably looks like complete gibberish right now. So heres a real example. It takes a number and doubles it, returning the number that has been doubled.


\begin{lstlisting}
public int doubleNumber(int numberToDouble)
{
return (numberToDouble * 2);
}
\end{lstlisting}

If you stick this into an example class in BlueJ and create a new object from your class, you can call the method doubleNumber by right clicking on the object on the object bench and selecting the doubleNumber method. Give it a nice integer, hit okay and see what happens. Shock, horror...it doubles your number! This is not really a great example of a method because its pretty useless, but it at least gives you an idea of what methods can do.

It is very important to note that methods like this need a return statement somewhere in them - in this case it was "return (numberToDouble * 2);". Anything typed after the return statement will not be executed and the java compiler will in fact throw out an error at you saying something along the lines of "unreachable statement".

The datatype that is returned can be any from the list above and also you can return Objects using methods. However, we havent really covered object interaction yet so that'll come a bit later on. There is a single exception to the "must have a return statement!" rule, and that is that you may specify the type to be void. This means you dont need to return anything. An example of when you might want to use a void instead of some value is for editing one of your class variables. The example below may help you to understand this:


\begin{lstlisting}
public class NameOfClass
{
public int helloWorld;
\par
Na...
...alue)
{
helloWorld = newValue;
return helloWorld;
}
}
\end{lstlisting}

So, when you make an object with this code then when it is created it'll have a value helloWorld to 1. If you call the first method (setHelloWorldValue) then it'll set the value of helloWorld to whatever you specified. If you call the second method (setHelloWorldValueAndReturnNewValue) then the variable will be set to whatever you specify and then returned. To test this out in bluej, copy out the code above, create a shiny new object and use its methods on the object workbench. You'll see that the first one doesnt appear to do anything (But inspect the object to check, because it does!) and that the second one will give you some feedback about the number returned.

This type of method which changes a Class variable is known as a Mutator. The other kind of method is called an Accessor and this just accesses the Class variable and does not do anything else. Below is a nice example of an Accessor.


\begin{lstlisting}
public class NameOfClass
{
public int helloWorld;
\par
Na...
...ic int exampleAccessorMethod()
{
return helloWorld;
}
}
\end{lstlisting}

The example mutator method here just returns helloWorld and this is all. Nothing more. No editing. So its an Accessor.


next up previous
Next: if Statements Up: The Frightened Freshers Guide Previous: Types
Tom Carlson 2006-01-10