Java Working With Class
Written by Ian Elliot   
Article Index
Java Working With Class
What's in a Class
Adding a Class
Why class and objects?
MathExample

Why bother with class and objects?

If this is the first time you have met the idea of object-oriented programming you might be wondering why we go to so much trouble.

Why bother defining a class just to create an instance of it?

All it seems to do is create a multiplicity of names.

While some languages do take a more direct approach and avoid introducing the idea of a class, JavaScript for example, this classical approach to objects has many advantages.

Unfortunately, many of these advantages only become apparent when you do more complicated things, but even at a simple level you should be able to see why it is worth the bother.

For example, at the moment our program only contains a single example of the point class but there is no reason why we cannot “stamp out” others.

If you want an object to store the old location of something you might write

Point Old =new Point();

and now you have two point objects, Old and Current. You could store values in either using lines like Old.x=15 and Current.x=25 and so on.

Once you have a class definition you can reuse it over and over again within the same program.

It is also important to know that there are more sophisticated reasons for using  classes and objects but for the moment let's concentrate on the directly practical.

Methods

If this doesn’t seem much justification you need to recall that a class definition can include data members and function members.

If you have followed the argument so far let me warn you that it is when function members are introduced that people tend to get lost.

There is something natural and understandable about stamping out multiple copies of data members, but to treat function members in this way just seems wrong.

The key factor is that as far as you, the humble programmer, is concerned function members behave in exactly the same way as data members. 

For example, suppose we want to use a standard operation on a point object which swaps the x and y coordinate over. One way of doing this is to introduce a Swap member function.

public class Point {
    public int x;
    public int y;
    public void swap() {
        int temp=x;
        x=y;
        y=temp;        
    }
}

 

If you concentrate on swap() for a moment you will see that it is just a little bit of Java code - it declares the variable temp and then uses it to swap the contents of x and y - wrapped up under the name swap. Also, notice that as this is a class definition none of the variables or code exists, this is still just a template for stamping out examples of the class. 

Now if we use the instruction

Point Current = new Point();

an instance of the class is created, complete with variable and swap member function.

How do you use the swap member function?

Easy, in exactly the same way you would use the data members. That is,

Current.swap();

will swap the values in the x and y data members in the object called “Current”.

If you have another point object called Old then

Old.swap();

will swap the values stored in Old’s data members.

Yes, you can treat each object as if it had its own completely separate copy of the Swap function just like it has its own copy of x, y and any other data member you may have declared.

One subtle point - why did the definition of the Swap function not have to say which x and y variables it wanted to use? It just mentioned x and y, not Old.x or even point.x. The answer is that in a class definition you can use the member names without having to qualify them by the class name or an instance name.

If you want to know the full details then a class does have a way to refer to itself. The special identifier "this" is used to mean the current instance. Notice that you can't actually use an instance name in a class because you don't know what it is. A class can be used to stamp out any number of instances. The "this" identifier may seem strange at first but it makes sense when you think about it meaning "this instance" rather then "this class" - which is a name you do know. 

So you could have written the swap method as:

public class Point {
    public int x;
    public int y;
    public void swap() {
        int temp=this.x;
        this.x=this.y;
        this.y=temp;        
    }
}

The rule is that when ever you refer to a variable like x then it is assumed that you mean this.x. In most cases you can avoid having to write "this" and keep things simple. The only problem is that occasionally you do need to use "this" to make an ambiguity clear and then it might be so long since you used it that you have forgotten what it is all about. 

Also notice that temp is a variable declared and used only by the swap method. It only exists while Swap is doing its stuff and it isn’t accessible to the outside world in any way. In this sense it belongs to the swap method rather than the object instance or class and so there is no need to use this.

Only use this to make clear that you mean a variable that is a property or member of a class.

As you might guess there are also private members. If you use private in place of public then the member if only accessible within the class. You can use this to create variables and methods that are only for use within the class. 

Method functions are complete little programs in their own right and can do anything that you can specify in Java.

Summary

This has been a complicated chapter and it is worth making a list of the new ideas.

  • Objects are a way of grouping together data and the functions that process them. They are also a natural way to write software that models the way the world works.

  • You can define a class which is a Java program in miniature used to create instances of objects. A class is defined as

          public classname{
           code that defines the class

          }

  • In Java is is usual for each class to be stored in a separate file of the same name.

  • When you have a class you can create any number of instances of the class using instructions like:

        myclass variable = new myclass();

  • Any variables or functions that are declared public are accessible from outside of the class or instance using a fully qualified name:

         myobject.variable;
         or
         myobject.function();

  • Any variables or function declared private can only be used within the class and cannot be accessed outside of the class.

  • Public is the default for all declarations.

  • Methods can refer to members in the current instance using the key word "this" but in most cases this is the default.

largecover