|Java Working With Class|
|Written by Ian Elliot|
Page 2 of 4
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 only this time make sure that the "Create Main Class" selection box is ticked.
Now that we are starting to learn about class and object we might was well start making use of them. The only new feature is that now you will find a default class created for you with the same name as your project.
For example, if you called the project PointOne then a class called PointOne will be created for you complete with a main method. 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.
You might be puzzled as to why you have a main method in a class called PointOne 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 the same name a s the project 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.
Before we start adding code to the main method however we need to create the Point class so we can use it.
It is usual in Java to store each class you create in a separate file of the same name. This makes managing the project easier and has other advantages.
The easiest way of doing this using NetBeans is to use the New command. Right click on the folder that the main class is stored in and select New and then Java Class:
Just give the class the name Point and you will see that a separate file has been created called Point.java.
This one class to one file of the same name is a peculiarity of Java and if you have worked with another object oriented language you might find it difficult to get used to at first.
To enter the simple code for the Point class all you have to do is double click on the file and when if opens in the editor enter:
You will discover that the "public class Point" line has already been entered for you.
Now move back to the main class and enter:
This simply creates an instance of the class “Point”, stores something in each of its data members and then prints them out to the Output window just to show that x and y really do exist and work as variables.
If you run the program you should see 10 and 20 in the Output window.
A Java Idiom - Creating Objects
Instead of a two-step process - create a variable and then store an object in it, you can use a one-step object creation process that is a common way of writing things in Java and other object oriented languages.
The two lines
can be combined into
The only confusing part is the way the identifier “Point” occurs at both ends of the instruction. The first point gives the type of the variable Current and the second is the type of object you are creating to store within Current.
In fact things are a little more complicated than this. Point() is infact a function called the constructor. The idea is that every class defines a function called the constructor which, as its name suggests, is responsible for creating instances of the object. You can think of:
as being a function call that creates an instance of the Point class. Later we will find out how to create custom constructors that do jobs like initialization.
Public and private
A class defines a program in miniature. It can have variables and any number of functions that you care to define. However there is one important idea that is new. When you declare a variable or a function then you have a choice. You can declare it to be public, in which case it is accessible from outside of the class or you can declare it to be private in which case it isn't. If you don't use public or private then the default is public.There are other options but for the moment it is enough to consider these two cases.
Anything that you declare as public is a property of the class or rather of the object it creates. In the previous example when we wrote:
Then both x and y are public properties of the class and can be accessed from outside of the class using a fully qualified name e.g.
If x had been declared as
then any attempt to access it as Current.x would have failed with an error message. The private variable can be used by any of the functions within the class but it isn't accessible from outside of the class.
The basic idea here is that a class is a bag of Java code that can be used by other Java programs. However you don't necessarily want the outside world to see the internal workings of this bag of code and so you use private variables and functions. The variables and functions that you do choose to make public define the characteristics of the objects the class creates to the outside world - it defines the objects interface with other objects.
You also need to remember that any variables defined with a function are local to that function and have nothing to do with any variables of the same name anywhere else in the program.
If this all seems complicated it soon settles down to being second nature.