Java Working With Class |
Written by Ian Elliot | ||||||
Page 3 of 5
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 again. 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 principle of "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, and select Main as the Main class, you should see 10 and 20 in the Output window. A Java Idiom - Creating ObjectsInstead 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: new Point(); 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 privateA 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.
|