next up previous
Next: Types Up: The Frightened Freshers Guide Previous: Class Variables

Constructors

Now that you know how to define a mould for your objects (The Class) and how to define some variables that will be visible to the whole class, why not start by doing something with these variables as soon as the object is created from the class? Things that happen as soon as the object is created are done in the Constructor. In the example in the previous section, we defined a new Class variables called "helloWorld". So what if we wanted this to be set to a certain value when the Object was created? Well, its as easy as:


\begin{lstlisting}
public class NameOfClass
{
public int helloWorld;
\par
Na...
...ass
{
helloWorld = 1;
}
// Other stuff goes in here
}
\end{lstlisting}

So, this will set the variable named helloWorld to equal 1. You dont know much about variable types right now so that will be in the next section. Read on, or forever be left wondering what on earth an "int" is!



Tom Carlson 2006-01-10