Java Working With Class
Java Working With Class
Written by Ian Elliot   
Monday, 30 July 2012
Article Index
Java Working With Class
Hands-on example
Why class and objects?

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 objects but for the moment let's concentrate on the directly practical.


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;


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,


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


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.

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. Method functions are complete little programs in their own right and can do anything that you can specify in Java.


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

  • 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:


  • 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.

Last Updated ( Thursday, 09 June 2016 )

RSS feed of all content
I Programmer - full contents
Copyright © 2016 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.