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

Defining A Class

Heres where it begins - defining a class. Classes are the building blocks of Java, and you really cant do anything at all without defining a class (I mean it! You cant type any code!). Classes are used to make objects, so a good way of thinking about a class is like a Jelly (Or Jello, if you are American) mould that you can create lots of Jellys (Objects) from. An example of this is a car class from which you could create as many car objects as you like. As for the actual code to define a class, it is as follows: language=Java
\begin{lstlisting}
public Class NameOfClass
{
// Class Variables go here
/...
...
// Accessor methods go here
// Mutator methods go here
}
\end{lstlisting}

...And thats it. Simple? Yep! Some notes about the Class definition: You need to state "public" before saying the name of the Class. Next, the class name itself is capitalised. The reasons for this will become clear later on, but basically its so that it is easier to distinguish between your Classes and Methods inside of the class (Which should start with a lowercase letter). Finally, you need to note that there are curly brackets around the main body of the code (The Constructor, Accessors and Mutators). This system of having curly brackets around bits of code happens quite a bit in Java, and some of the time the Java compiler might moan at you for including too many or too few brackets. To help stop this from happening, it is a good idea to make sure you indent your brackets.

The example above will compile okay, but it wont actually do anything. If you are using BlueJ, then you'll be able to create objects but they will be pretty useless...In the section after the next one, I'll show you how to do things with the Class, but first you need to know a little bit about Variables. If you've programmed in pretty much anything else ever then you'll know enough about Variables and can skip the section.


next up previous
Next: Class Variables Up: The Frightened Freshers Guide Previous: Introduction
Tom Carlson 2006-01-10