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?
MathExample

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 tick the "Create Main Class" selection box. 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.

Before we start adding code to the main method however we need to create the class. It is usual in Java to store each class you create in a separate file of the same name. 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:

 

newclass

 

 

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:

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

 

You will discover that the public class Point line has already been entered for you.

Now move back to the main class and enter:

public static void main(String[] args) {
        Point Current;
        Current = new Point();
        Current.x = 10;
        Current.y = 20;
        System.out.println(Current.x);
        System.out.println(Current.y);
 }

 

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

The two lines

  Point Current;
  Current = new Point();

can be combined into

Point Current = new Point();

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.

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:

   public int x;
   public int y;

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.

Current.x = 10;

If x had been declared as

private int x;

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.



Last Updated ( Wednesday, 07 November 2012 )
 
 

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