Class Structure and SyntaxHomepage  « Learn Java6 « Class Structure and Syntax

In the last lesson on Arrays we introduced some new concepts such as instantiating an instance of a class using the new keyword and using a class member via anArray.length. We will expand on these topics as we go through this section but firstly lets take a look at the structure and syntax of a class.

A Look At Class Structurego to top of page Top

A class is made up of members that form a blueprint for an object and its behaviour. Before we go into more details lets look at a diagram showing the generic form of a class:

class structure

So using the diagram above we can note down a few points about a Java class:

  1. We define a class using the class keyword followed by the name of the class and a set of curly bracers.
  2. We can specify one or more instance variables within the curly bracers of the class and each must end with a semi-colon.
  3. We can specify one or more methods within the curly bracers of the class, after any instance variables, with each method also having a set of curly bracers.
  4. The void keyword preceding the name of each method specifies that the method doesn't return any value. This is known as a return type and is mandatory.
  5. We can specify one or more statements within the curly bracers of each specified method and each must end with a semi-colon.

There is more to classes than the points raised above but for now this is more than enough to be thinking about. The reason variables defined within a class, and outside any method within the class, are called instance variables is that they belong to instances of a class. If we think of an object as an instance of a class, then the instance variables belong to individual instances (objects) of the class. So each time we create a new instance (object) of the class, that instance gets its own set of instance variables. The variables are completely unique to each instance and the values held within each instance are known as object state. So getting back to our class structure we can say that a class is not an object but is used to construct an object instance.

Defining A Classgo to top of page Top

In the previous section we created a lot of classes that contained a main() method to focus on programming features of Java and so the classes weren't created for a specific purpose. As we mentioned at the start of this section a well designed class should define a logical entity that fits a particular requirement within the application as a whole. To highlight what we mean by this lets look at the syntax of a simple class that has a particular purpose in mind.

class syntax

The simple Cat class above has four members, the three instance variables name, colour and age and the method talk(). The three instance variables name, colour and age are the things we know about a Cat object (object state). The talk() method is something a Cat object does (object behaviour).

Ok lets code up this simple class.

  A Cat Class
public class Cat {
    String name, colour;
    int age;

    void talk() {

Save and compile the file in directory   c:\_ObjectsAndClasses in the usual way.

Once the class is coded we need a way to test it. Time for our first test class using our trusty old friend the main() method. So lets create a test class to make sure our Cat class works:

  Test Class for Cat
public class TestCat {

    public static void main (String[] args) {
        Cat moggy = new Cat();    // Create a Cat instance = "Tigger";    // Use dot notation to populate cats name
        moggy.colour = "Orange";  // Use dot notation to populate cats colour
        moggy.age = 12;           // Use dot notation to populate cats age
        System.out.println("Our " + moggy.colour + " cat called " + + " is " + moggy.age);;             // Use dot notation to execute the talk() method

Save, compile and run the file in directory   c:\_ObjectsAndClasses in the usual way.

run test Cat

The above screenshot shows the output of running our TestCat class. We created a Cat instance called "moggy" and then gave our cat a name, colour and using dot notation. Finally we called the talk() method to make our cat meow.

Class Structure Quizgo to top of page Top

Try the quiz below to test your knowledge of this lesson

Question 1 : What are instance variables and class methods commonly known as?
- Instance variables and class methods are commonly known as <em>members</em>.
Question 2 : What describes an object's state?
- Instance variables describe an object's state.
Question 3 : A Class must have at least one instance variable?
- There is no requirement for a class to have any instance variables
Question 4 : What describes an object's behaviour?
- Class methods describe an object's behaviour.
Question 5 : How do we instantiate an object?
- We instantiate an object using the <code>new</code> keyword.
Question 6 : How do we access class members?
- We access class members using dot notation.
Question 7 : A class is an object?
- A class is a blueprint for object construction.
Question 8 : Instance variables apply to the whole class?
- Instance variables apply to each instance of a class, not to the whole class.
Status Bar Please select an answer

What's Next?

In the next lesson we continue our study of Java objects and classes by looking at reference variables.

<<  Arrays                    Reference Variables  >>

go to home page Java6 Tutor Homepage go to top of page Top