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

What's in a Class?

A class definition looks a lot like the sort of Java code we have been writing up to this point. This is a good thing because it looks familiar but it is also a bad thing because you need to keep in mind that you can't acutely run the code in a class unless you first create an instance of it.

That is a class definition is a template for a chunk of program. It can include variables and functions but when you write it the program doesn’t actually exist anywhere all you have is a recipe for making the program when you want to.

For example, if you write a simple class called Point which is going to be used in our programs to represent a 2D point with an x and y co-ordinate. That is a point object is going to store two numbers representing x and y as properties of the object, its class definition would be -

public class Point {
    public int x;
    public int y;   
}

 Don’t worry for the moment about the use of public, all this means is that class and variables are accessible from outside of the class. 

Concentrate on the fact that what you have defined here is a template that can be used to “stamp out” multiple copies of this code object.

At the moment there is no variable called x and no variable called y allocated in memory waiting for you to use them. To make something happen you have to take the class definition and use it to create an example, or an "instance", of the class or an object. 

Unfortunately in Java creating an object is a bit more complicated than creating a simple variable but the process is more or less the same once you have untangled it. Firstly you need a variable of the right type to store the new object. This you create just as you would any other variable.

For example,

Point Current;

declares a variable called Current suitable for storing a point object i.e an instance of the Point class. 

Compare this with

int x;

which declares a variable suitable for storing an integer.

After the variable has been declared it doesn’t actually contain an example of the class - just in the same way x doesn't contain an particular integer. It’s just ready and waiting to store a new class and the keyword “new”  is exactly how you go about making an instance of the class.

For example

Point Current;
Current=new Point();

creates an instance of the class and stores it in Current

Properties

We have already met properties as part of our look at Swing components but there is a more basic form of property that we need to master first.

We have just created an instance of the Point class. Now there are two integer variables, called x and y, waiting for you to store data in them but you don’t refer to them just as x and y. To gain access to any of the components or “members” of an instance of a class you have to specify the particular instance name as well as the member name.

Put more directly to store something in x you would write

Current.x=10;

In general when you create an instance called myobject you refer to any of its members using a fully qualified name:

myobject.myproperty

If you are wondering why it isn't

Point.x=10;

then you need to remember that it is the instances you create from the Point class that are objects you can work with. Point is simply a template that you can use to create Point objects.

Point is a class not an object.

A small example

At this point, no pun intended, you might like to try a simple example, just to make sure it all works.

There are also some practical matters we have to deal with about how you create a new class within the NetBeans IDE.

First start a new Java project in the usual way.

Now that we are starting to learn about class and object we might was well start making use of them and add a main class. Earlier versions of NetBeans added a main class for you automatically but in the latest versions 10 and later you have to do it manually. Right click on Source Packages and select New, Java Main Class...

main

You can leave the defaults as they are or change the name of the class from NewMain to just Main. When you look at the code generated you will find:

public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }    
}

As you might guess the main method is where your code starts when the program is run - so this is the place to put your Java code to get the program going.

You might be puzzled as to why you have a main method in a class called Main and not an object. Things are always more complicated than an initial explanation covers. In this case the complication is that classes can have methods that can be executed without creating an instance. These are the so called static methods and we will have more to say about these later. For the moment all you really need to know is that your Java program has to have a starting point some where and the usual solution is to create a class with a suitable name and within it create a single static method. This is then run when you run the project without having to create an instance of the main class. There are other ways of doing the same job but this is the most common.